Selenium error when interacting with textarea - python

I try to enter text into text area on the web i am using selenium to do this but when i try to input into textarea it fails with error:"selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable".
However when i try to put text into field it works normaly
driver.find_element_by_xpath('//textarea[#class = "Ypffh"]').send_keys(text);

You could try it with javascript:
driver.execute_script('document.querySelector("textarea.Ypffh").innerText = "xxx"')

Sometimes you might need to click on the textarea before you can interact with it.
I would try this:
driver.find_element_by_xpath('//textarea[#class = "Ypffh"]').click();
driver.find_element_by_xpath('//textarea[#class = "Ypffh"]').send_keys(text);
If that doesn't work, I would check to see if there's a hidden tag near the textarea that may function as the input receiver rather than the textarea itself. Sometimes you see cases where a textarea is just a visual representation, but doesn't actually receive text, so there may be a hidden that receives text instead.

Related

How do i select element in a list present in an un-ordered list using selenium python

this the website I am dealing with https://www.bseindia.com/corporates/ann.html?curpg=1&annflag=1&dt=20211021&dur=P&dtto=20211027&cat=Insider%20Trading%20/%20SAST&scrip=&anntype=A
Here I am able to send the code for the "security name" but in-order to submit it I need to click the dropdown element that comes after giving the security name. How do I achieve this with selenium. I used the code below and its not working(StaleElementReferenceException)
security_name = driver.find_element_by_id("scripsearchtxtbx")
security_name.send_keys('INE350H01032')
sec_click = driver.find_element_by_xpath('//*[#id="ulSearchQuote2"]/li')
sec_click.click()
This can also be accomplished using the Keys Library within Selenium. Selenium Keys not only sends input statements like strings, but it can also send commands such as escape, tab, or in this case enter. Your updated code should look as follows:
security_name = driver.find_element_by_id("scripsearchtxtbx")
security_name.send_keys('INE350H01032')
security_name.send_keys(Keys.ENTER)
sec_click = driver.find_element_by_xpath('//*[#id="ulSearchQuote2"]/li')
sec_click.click()
These are called special keys. For more examples and more information on this, see this link
you can paly with Xpath:
security_name = driver.find_element_by_id("scripsearchtxtbx")
security_name.send_keys('INE350H01032')
sec_click = driver.find_element_by_xpath("//ul[#id='ulSearchQuote2']/li//strong[text()='INE350H01032']")
sec_click.click()
same -->("//ul[#id='ulSearchQuote2']//strong[text()='INE350H01032']")
you can check more about xpath here: xpath

Find a Button without xPath, ID, or Name in Python

I am trying to find a Submit Button on a webpage, however using xPath is not working to find it for some strange reason, and it has no class, name, or id. The line itself in the webpage is below.
The xPath is
/html/body/div[3]/form/input[2]
but like I said, it can't find it this way for some reason. The selector is
body > div.pagebodydiv > form > input[type="submit"]:nth-child(5)
I just need to press this button and I can't figure out how. Please help.
It is the Submit Button
You could try using '//', meaning can be found here https://www.w3schools.com/xml/xpath_syntax.asp
xpath_selector = "//input[#value = 'Submit']"
OR:
xpath_selector = "//input[#value = 'Submit' and #type = 'submit']"
OR
xpath_selector = "//input[starts-with(#value, 'Submit') and starts-with(#type, 'submit')]
The way your are currently focusing on could easily cause error if page change a tiny bit in structure, moreover, selecting from root takes a lot of effort.

Getting visible text Selenium [duplicate]

How do I get the visible text portion of a web page with selenium webdriver without the HTML tags?
I need something equivalent to the function HtmlPage.asText() from Htmlunit.
It is not enough to take the text with the function WebDriver.getSource and parse it with jsoup because there could be in the page hidden elements (by external CSS) which I am not interested in them.
Doing By.tagName("body") (or some other selector to select the top element), then performing getText() on that element will return all of the visible text.
I can help you with C# Selenium.
By using this you can select all the text on that particular page and save it to a text file at your preferred location.
Make sure you are using this stuff:
using System.IO;
using System.Text;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
After reaching the particular page try using this code.
IWebElement body = driver.FindElement(By.TagName("body"));
var result = driver.FindElement(By.TagName("body")).Text;
// Folder location
var dir = #"C:Textfile" + DateTime.Now.ToShortDateString();
// If the folder doesn't exist, create it
if (!Directory.Exists(dir))
Directory.CreateDirectory(dir);
// Creates a file copiedtext.txt with all the contents on the page.
File.AppendAllText(Path.Combine(dir, "Copiedtext.txt"), result);
I'm not sure what language you're using, but in C# the IWebElement object has a .Text method. That method shows all text that is displayed between the element's opening and closing tag.
I would create an IWebElement using XPath to grab the entire page. In other words, you're grabbing the body element and looking at the text in it.
string pageText = driver.FindElement(By.XPath("//html/body/")).Text;
If the above code does not work for selenium, use this:
string yourtext= driver.findElement(By.tagName("body")).getText();

Having trouble inputting into tinymce via selenium

In selenium i am trying to enter text into the tinymce text area, but i am having trouble selecting the text area to input the text. Is there a way to select the text area behind tinymce or anyway to select tinymce so i can enter text. thanks
Use command: runScript
Target: tinyMCE.get('text_area_id').setContent('Your text here')
or you can use tinyMCE.activeEditor.setContent('Your text here') which will select either the first or the last mceEditor, I forget..
You may use
tinymce.get('your_editor_id').focus();
In case you have one single editor on your page you may also use
tinymce.editors[0].focus();
driver.execute_script("tinymce.get('{0}').focus()".format('id_of_textarea'))
driver.execute_script("tinyMCE.activeEditor.setContent('{0}')".format('your text here'))
it works for me
(cant remember where i saw it thought)

Selenium, CSS, and Python: not being able to view default text in a textbox

I have a text field, which contains the default text 'abc'. When I write something inside this field or the cursor is inside this field then the default text disappears. The underlying HTML tag for this field is:
<div id="InputDefault" class="defaultText" style="visibility: visible;">abc</div>
Now, manually everything works fine. I am writing a test-case in Selenium to check if the default text appears once the field is empty. But in the test-case, it somtimes works and more often it fails. I have written the following function:
#I first delete the text in the field (I have used a text element)/
self.se.type(#I first delete the text in the field (I have used a text element).
self.se.type(locators["search_field_header"], "")
#Then I focus on the field.
self.se.set_cursor_position("headerParam", "")
#Then I focus on the field
self.se.set_cursor_position("headerParam",0)
#Press the TAB key to move focus away from the search field to get default text.
self.se.key_press_native("9")
self.se.set_speed(1000)
is_footer_default_text_present = self.is_element_available("InputDefault")
But this is somehow not working. What is the problem and is there a better/robust solution?
Use selenium.is_visible(locator). If it doesn't work then use selenium.is_text_present(value) or if (Expectedvalue == selobj.get_value(parameter)).
If it still doesn't work, refresh the page and try the above.

Categories

Resources