33

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?

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
ti01878
  • 483
  • 1
  • 5
  • 18

2 Answers2

34

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")
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • Yes, it is! Thank you. I have to say that you previous question was good and i found it by myself on the website: http://selenium-python.readthedocs.org/en/latest/navigating.html#moving-between-windows-and-frames Again, big thanks to you all. – ti01878 Jun 23 '14 at 17:21
7

Add some delay to the driver so that elements will load.

import time
time.sleep(2)
department_element = driver.find_elements_by_id("__id_name__")

or else you can use the following code so that loop runs until the element render

while len(driver.find_elements_by_id("__id_name__")) == 0:
    pass
department_element = driver.find_elements_by_id("__id_name__")