Sisqo Baker
Sisqo Baker

Reputation: 15

Python selenium cannot find element even with wait

I am trying to send text to an input field, but selenium is not able to find the element.

element = WebDriverWait(b, 10).until(EC.presence_of_element_located((By.XPATH, '/html/body/table/tbody/tr[1]/td/form/div/table/tbody/tr[2]/td/table[2]/tbody/tr/td[4]/table/tbody/tr/td[1]/input')))
element.send_keys("Customer Care", Keys.ENTER)

I've tried using the XPATH, the full XPATH and the ID to locate it, but it keeps giving me an error that indicates that it cannot find the element selenium.common.exceptions.TimeoutException

A snippet of the HTML element

<input class="iceInpTxt testBox" id="headerForm:jumpto" maxlength="40" name="headerForm:jumpto" onblur="setFocus('');iceSubmitPartial(form, this, event);" onfocus="setFocus(this.id);" onkeyup="iceSubmit(form,this,event);" onmousedown="this.focus();" type="text" value="">

Upvotes: 0

Views: 320

Answers (3)

undetected Selenium
undetected Selenium

Reputation: 193308

If your usecase involves invoking click() or send_keys() while inducing WebDriverWait instead of presence_of_element_located() you need to use the expected_conditions as element_to_be_clickable() as follows:

So effectively, you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(b, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.iceInpTxt.testBox[id^='headerForm'][name$='jumpto']"))).send_keys("Customer Care", Keys.ENTER)
    
  • Using XPATH:

    WebDriverWait(b, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='iceInpTxt testBox' and @id='headerForm:jumpto'][@name='headerForm:jumpto']"))).send_keys("Customer Care", Keys.ENTER)
    

References

You can find a couple of detailed discussion about the different expected_conditions in:

Upvotes: 0

David Warshawsky
David Warshawsky

Reputation: 142

it is a good idea to check whether or not you installed and imported selenium or other necessary packages. Use pip to check your version and see if there is a bug online. Please let me know what python version you are using. It is likely the XPATH you provided was incorrect or maybe try increasing the amount of time in the 2nd parameter of WebDriverWait(1st,2nd). It would be much more helpful if you had a link to this html page so I could check your XPATH. If you'd like further help, please provide your html page.

Edit: This is something that needs to be reproduced so that it can be checked. If you have tried the above, I am unable to help unless I see the html document. You should remove all sensitive information before sharing it. The other elements of your code seem to be correct.

Upvotes: 0

Sers
Sers

Reputation: 12255

Element has ID, use it as locator. Check if element is inside a iframe:

wait = WebDriverWait(b, 10)
element = wait.until(EC.element_to_be_clickable((By.ID, 'headerForm:jumpto')))
element.send_keys("Customer Care", Keys.ENTER)

How to switch to iframe:

wait = WebDriverWait(b, 10)

wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe_locator")))

element = wait.until(EC.element_to_be_clickable((By.ID, 'headerForm:jumpto')))
element.send_keys("Customer Care", Keys.ENTER)

# How to go back to default content
b.switch_to.default_content()

Upvotes: 1

Related Questions