W. Enzo
W. Enzo

Reputation: 3

Python Selenium Explicit WebDriverWait function only works with presence_of_element_located

I am trying to use Selenium WebDriverWait in Python to wait for items to load on a webpage , however, using any expected condition apart from presence_of_element_located seems to result in the error

selenium.common.exceptions.WebDriverException: Message: SyntaxError: missing ) in parenthetical

I thought it might be linked to the site I was trying against , however I get the same error on any site - see snippit below where I have replaced presence_of_element_located with visibility_of_element_located and am trying to confirm visibility of the search box on python.org.

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

driver = webdriver.Firefox()
driver.get("http://www.python.org")

try:
    element = WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.NAME,"q")))
    element.send_keys("pycon")
    element.send_keys(Keys.RETURN)
finally:
    driver.quit()

The Full stack trace is as below , Any help would be appreciated !

Traceback (most recent call last):
  File "C:\dev\test.py", line 51, in <module>
    element = WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.NAME,"q")))
  File "C:\Development\python\python35-32\lib\site-packages\selenium-3.0.0b3-py3.5.egg\selenium\webdriver\support\wait.py", line 71, in until
    value = method(self._driver)
  File "C:\Development\python\python35-32\lib\site-packages\selenium-3.0.0b3-py3.5.egg\selenium\webdriver\support\expected_conditions.py", line 78, in __call__
    return _element_if_visible(_find_element(driver, self.locator))
  File "C:\Development\python\python35-32\lib\site-packages\selenium-3.0.0b3-py3.5.egg\selenium\webdriver\support\expected_conditions.py", line 98, in _element_if_visible
    return element if element.is_displayed() == visibility else False
  File "C:\Development\python\python35-32\lib\site-packages\selenium-3.0.0b3-py3.5.egg\selenium\webdriver\remote\webelement.py", line 353, in is_displayed
    self)
  File "C:\Development\python\python35-32\lib\site-packages\selenium-3.0.0b3-py3.5.egg\selenium\webdriver\remote\webdriver.py", line 465, in execute_script
    'args': converted_args})['value']
  File "C:\Development\python\python35-32\lib\site-packages\selenium-3.0.0b3-py3.5.egg\selenium\webdriver\remote\webdriver.py", line 236, in execute
    self.error_handler.check_response(response)
  File "C:\Development\python\python35-32\lib\site-packages\selenium-3.0.0b3-py3.5.egg\selenium\webdriver\remote\errorhandler.py", line 192, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: SyntaxError: missing ) in parenthetical

Update - > After a few comments below i have done some testing on versions and browsers and this issue seems isolated to Python 3 and Firefox , the script works with Python 2.7 and works on both versions of python for Chrome webdriver .

Upvotes: 0

Views: 4026

Answers (2)

Aaron
Aaron

Reputation: 2393

These minor changes work for me.

  1. visibility_of_element_located ===> presence_of_element_located
  2. driver.quit() ===> driver.close()

See the following:

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

driver = webdriver.Firefox()
driver.get("http://www.python.org")

try:
    element = WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.NAME,"q")))
    element.send_keys("pycon")
    element.send_keys(Keys.RETURN)
finally:
    driver.close()

Upvotes: 1

saurabh baid
saurabh baid

Reputation: 1877

Copy pasted the same code and it works. Dint have enough repo to post comments so had to put it in answer section.

Upvotes: 0

Related Questions