I need to type some text in online notepad but cannot find the right element to send it.You can find whole page here
I try: fox.find_element_by_xpath(".//*[#id='tinymce']/p")
but error occured:
selenium.common.exceptions.NoSuchElementException: Message: Unable to
locate element: {"method":"xpath","selector":".//*[#id='tinymce']/p"}
Also I try:
fox.find_element_by_xpath("//div[contains(.,'Working')]") no errors but it didnt type the string
Since the 'notepad' is in an iframe, you first need to switch to the specific frame. Try this:
fox.switch_to.frame(fox.find_element_by_id("page_text_ifr"))
notepad = fox.find_element_by_id("tinymce")
notepad.send_keys("Hello World!")
Or since you use XPath:
fox.switch_to.frame(fox.get_element_by_xpath("//iframe[#id='page_text_ifr']")
notepad = fox.get_element_by_xpath("//body[#id='tinymce']")
notepad.send_keys("Hello World!")
Related
I am trying to click on the "Next Page"-Button on the Web of Science Search-Site to iterate through all pages.
Here is a screenshot of the HTML of the page (highlighted is the button)
This is my code to find the button:
driver.find_element_by_class_name('mat-focus-indicator mat-icon-button mat-button-base').click()
But I receive this error:
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".mat-focus-indicator mat-icon-button mat-button-base"}
I have tried so many ways of identifying the button (find_by_id, find_by_name, find_by_link_text) but nothing works.
What am I doing wrong?
Thank you in advance
Maybe try with Query Selector like:
driver.find_element_by_css_selector('button[data-ta="next-page-button"]')
You can always try selectors on elemnts panel (like on screenshort) and type your selector in field "Find by string, css, or xpath"
Class name do not have support for spaces, remove spaces and put . to make it css selector :
driver.find_element_by_css_selector('button.mat-focus-indicator.mat-icon-button.mat-button-base').click()
also try to put some wait before this to make it more consistent.
Currently I'm attemping to switch from my default content, to the only iframe in the website. I don't know if it's how the site is coded, but I can't access via DOM any element.
This is the HTML structure of the site:
XPATH of site is //*[#id="iframeBody"] (When I paste this in the element inspector, I get the correct iframe). So, if I try to switch using frame_to_be_available_and_switch_to_it, this is the output:
try:
WebDriverWait(self.driver, 10).until(ec.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#id='iframeBody' and #name='body']")))
except Exception as e:
print(e)
>>> Message: javascript error: Failed to execute 'getComputedStyle' on 'Window': parameter 1 is not of type 'Element'.
>>>(Session info: chrome=79.0.3945.88)
I also tried creating an iframe element variable, finding it via ID and XPATH and then I've used switch_to(element). Getting the same result. When I print this variable, the element is actually found:
# Also tried finding with id
element = self.driver.find_element_by_xpath('//iframe[#id='iframeBody')
print(element)
<selenium.webdriver.remote.webelement.WebElement (session="9184691b1fdcccc15dd36bbcb914ac8b", element="1ef77729-8a6e-4d3c-98bd-c95878437585")>
But when I try to switch to this iframe, I get the same result as above.
For some reason, this site is not letting me use the DOM data, actually, when I try to click a button I need to use action chains, because I get the same error.
Anybody can help me?
Did you put enough time before switching ?
also, you could directly switch as well:
driver.switch_to.default_content()
driver.switch_to.frame(driver.find_element_by_id("iframeBody"))
I need to find the xpath for the line highlighted in the image; the page is dynamic.
I tried this:
//td[contains(text(),'Ricardo')]
But gave me an error
selenium.common.exceptions.NoSuchElementException: Message: no such
element: Unable to locate element:
{"method":"xpath","selector":"//td[contains(text(),'Ricardo')]"}
You may try:
driver.findElement(By.xpath("//*[text()='Ricardo']"))
I assume you know how to inspect the elements on a page. In the console window, you can then just right click the element you want and copy the Xpath. Once you've copied it, paste in following code: driver.find_element_by_xpath("paste here"). You might see that within the xpath there are double brackets and you will need to replace these by single brackets.
My xpath:
//span[#id=’lblError’]/text()
works correctly in developer tools in Chrome. When I use the same xpath in python 2.7 I get the following error:
Message: no such element: Unable to locate element {“method”:”xpath”,”selector”:”//span[#id=’lblError’]/text()”}”
In Python 2.7 my xpath is:
driver.find_element_by_xpath(“//span[#id=’lblError’]/text()”)
Also if I use ("//span[#id=’lblError’]") it does not work as well.
Also I am using python 2.7 with selenium webdriver.
Don't include text() in the XPath expression, separate it.
You can try the below option: element = driver.find_element_by_xpath("//span[#id='lblError']"); print element.text
If the above option doesn't work then try to give some delay before fetching it.
I am using python selenium on browser to fill in some form. I was trying to select an element in the drop-down list,
0
but if I try to find it by text using this script:
browser.find_element_by_link_text("0").click()
it result an error:
"unknown error: Element is not clickable at point (498, 612). Other element would receive the click: ..."
and if try to find it by class name:
browser.find_element_by_class_name("dropdown-toggle").click()
it result in another error: "element not visible"
is there any way I can click onto that drop-down list? Thanks very much.
I had a similar problem. You can execute a script to change the visibility of that element once you find it and then click it.
driver.execute_script("arguments[0].style.visibility = 'visible';",myElement)
myElement.click()
Try to find it by Xpath searching for partial class and text at the same time:
browser.find_element_by_xpath(//a[contains(#class, 'dropdown-toggle select') and contains(text(), '0')]).click();
you should get the element by xpath and then press it.
browser.find_element_by_xpath(xpath).
read here how to get the xpath: http://www.wikihow.com/Find-XPath-Using-Firebug
Thanks for the feedback, I've found the problem which was due to the new script being loaded by javascript as triggered by some clicks. I've created the following code to capture the exception and retrying until the element is ready:
while True:
try:
time.sleep(1)
browser.find_element_by_xpath("//div[#class='tckt']/a").click()
print("found")
break
except ElementNotVisibleException:
print("notvisible")
except WebDriverException:
print("clickduplicate")
I read it on articles says this happened a lot on radio button for Chrome webdriver, hope this helps.
Firstly click the parent element by finding it, using it's xpath->find the element you want to click within the parent element like the way you did. i.e
browser.find_element_by_link_text("0").click()
Hope this 2 steps will work for you. Otherwise please post the url with the code you've tried-It'll be easy to find out the issue.