7

I'm having trouble on figuring out how to use Selenium's wait function. What I'm trying to do is check to see if a element is present and if it is then it'll print a statement. Here is my code:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException

browser = webdriver.Firefox()
browser.get("http://google.com")
delay = 3 # seconds
try:
    WebDriverWait(browser, delay).until(EC.presence_of_element_located(browser.find_element_by_xpath('//*[@id="hplogo"]')))
    print ("Page is ready!")
except TimeoutException:
    print ("Loading took too much time!")

The current code gives me the following errors:

Traceback (most recent call last):
  File "/Users/John/Documents/waitTest.py", line 10, in <module>
    WebDriverWait(browser, delay).until(EC.presence_of_element_located(browser.find_element_by_xpath('//*[@id="hplogo"]')))
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/support/wait.py", line 71, in until
    value = method(self._driver)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/support/expected_conditions.py", line 59, in __call__
    return _find_element(driver, self.locator)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/support/expected_conditions.py", line 274, in _find_element
    return driver.find_element(*by)
TypeError: find_element() argument after * must be a sequence, not WebElement
John Pham
  • 183
  • 2
  • 2
  • 6

3 Answers3

15

Expected condition expects a tuple to be passed in where the first item is the locator's type (By constant) and the second is the locator's value:

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(browser, 10)
wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="hplogo"]')))

Note that in your case By.ID would be simpler:

wait.until(EC.presence_of_element_located((By.ID, 'hplogo')))
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • `from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.common.exceptions import TimeoutException browser = webdriver.Firefox() browser.get("http://google.com") delay = 3 # seconds try: wait = WebDriverWait(browser, 10) wait.until(EC.presence_of_element_located(('by.id', 'hplogo'))) print ("Page is ready!") except TimeoutException: print ("Loading took too much time!")` It's not printing the first print but it does wait the delay and prints the second. – John Pham Dec 29 '15 at 05:24
  • @JohnPham don't remember suggesting you using `'by.id'`. – alecxe Dec 29 '15 at 05:25
  • 1
    Having it as By.ID gives me this error: `raceback (most recent call last): File "/Users/John/Documents/waitTest.py", line 11, in wait.until(EC.presence_of_element_located((By.ID, 'hplogo'))) NameError: name 'By' is not defined` – John Pham Dec 29 '15 at 05:27
2

you need to import the By module

from selenium.webdriver.common.by import By
Shawn Mehan
  • 4,513
  • 9
  • 31
  • 51
1

if you are using chrome, then it works with

from selenium.webdriver.support import expected_conditions

The other imports are okay, does not need change regarding the browser.

Caridorc
  • 6,222
  • 2
  • 31
  • 46