Mahdi
Mahdi

Reputation: 1035

Python Selenium webdriver timeout exception

I'm working on a project to extract some data from a website. In this website there is search form that I should fill it. One of the inputs which is text, shows a suggestion after entering 2 or 3 characters and I should select that option in order to go forward or search button will be activated. The problem is that when I use the following code:

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='LocationSuggestionBox']/ul/div/li/div"))).click()

I modified the xpath in above code. The actual xpath is as fllow:

//*[@id="LocationSuggestionBox""]/ul/div/li/div

But I don't know how to add it in my code to not get the syntax error.

The final result with my working code is :

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='LocationSuggestionBox']/ul/div/li/div"))).click()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/support/wait.py", line 80, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 

Upvotes: 0

Views: 165

Answers (2)

KunduK
KunduK

Reputation: 33384

Induce WebDriverWait and element_to_be_clickable() And following xpath.

driver.get('https://locatr.cloudapps.cisco.com/WWChannels/LOCATR/openBasicSearch.do;jsessionid=8CDF9284D014CFF911CB8E6F81812619')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='searchLocationInput']"))).send_keys('China')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//li[@class='ng-scope']//span[text()='CHINA']"))).click()

Browser snapshot:

enter image description here

Upvotes: 1

CEH
CEH

Reputation: 5909

Your XPath is returning NULL when I run against the page, so the selector is incorrect here.

Based on the page info you provided, here's a correct selector:

"//li[div/span[text()='" + locationNameHere + "']]"

So you can change your code to:

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//li[div/span[text()='" + locationNameHere + "']]"))).click()

If you just want to click the first location suggestion, you can use this:

//li[div/span]

But this XPath will get you a list of ALL visible location suggestions.

Upvotes: 1

Related Questions