Deyan Georgiev Deyan Georgiev - 4 months ago 9
Python Question

Selenium fails to select element by id

Here is the HTML code of the element:

<input maxlength="64" name="pskSecret" class="text" id="pskSecret" value="" size="32" type="text">


And here is my python code, which tries to select it:

self.driver.find_element_by_id("pskSecret").clear()
self.driver.find_element_by_id("pskSecret").send_keys(data) # data is variable


However I got exception, stating that selenium is unable to locate the element.
Any ideas what may be causing the problem

Edit: Also the element is inside an iframe, however I'm accessing other elements in it which are working correctly.

Answer

May be when you are going to find element, it could not be load on the DOM due to timing issues. You should try using WebDriverWait to wait until element is visible as below :-

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC



wait = WebDriverWait(self.driver, 10)

input = wait.until(EC.visibility_of_element_located((By.ID, "pskSecret")))
input.clear()
input.send_keys(data)

Note :- if this element is inside any frame you need to switch that frame before finding element using self.driver.switch_to_frame("your frame id or name")