Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python with selenium: unable to locate element which really exist

I've been trying fill input:

<input id="PASSFIELD1" class="logField" type="password" onkeyup="next(this, event);" maxlength="1" autocomplete="off" name="PASSFIELD1"></input> 

To do this, I have to find this element.

I tried below things:

  1. pass1=driver.find_element_by_name("PASSFIELD1")

  2. pass1=driver.find_element_by_id("PASSFIELD1")

  3. pass1= driver.find_element_by_xpath("/html/body/div[4]/div/div/div[2]/div/form/div[3]/table/tbody/tr[3]/td[2]/div/input[1]") (path from firebug)

  4. Even wait 100 seconds for it

self.wait.until(EC.visibility_of_element_located((By.XPATH,"/html/body/div[4]/div/div/div[2]/div/form/div[3]/table/tbody/tr[3]/td[2]/div/input[1]"))) self.assertTrue(self.driver.find_element_by_xpath("/html/body/div[4]/div/div/div[2]/div/form/div[3]/table/tbody/tr[3]/td[2]/div/input[1]"))

I always get:

selenium.common.exceptions.NoSuchElementException: Message: 'Unable to locate element: (...)

Do you know what I am doing wrong?

like image 444
ti01878 Avatar asked Jun 23 '14 15:06

ti01878


People also ask

Does element exist in Selenium Python?

The easiest way to check if an element exists in a web page is with the Selenium webdriver find_elements_by_css_selector() function. If the element exists, then find_elements_by_css_selector() will return a list with those elements.

How to handle NoSuchElementException in Selenium python?

1. Change in source code of the webpage. It's possible that the source code of the webpage may have been changed since the last time you accessed it. Change your code according to the new source to solve the issue.

What is find element by XPath Selenium Python?

You can use XPath to either locate the element in absolute terms (not advised), or relative to an element that does have an id or name attribute. XPath locators can also be used to specify elements via attributes other than id and name.


1 Answers

The problem is that your input tag is inside an iframe, you need to switch to it first:

frame = driver.find_element_by_xpath('//frame[@name="main"]') driver.switch_to.frame(frame) pass1 = driver.find_element_by_id("PASSFIELD1") 
like image 162
alecxe Avatar answered Sep 25 '22 23:09

alecxe