Python Selenium显式WebDriverWait函数仅适用于presence_of_element_located

时间:2016-09-19 15:59:55

标签: python selenium

我正在尝试在Python中使用Selenium WebDriverWait来等待在网页上加载项目,但是,除 presence_of_element_located 之外的任何预期条件似乎都会导致错误

  括号中的

selenium.common.exceptions.WebDriverException:消息:SyntaxError:missing)

我认为它可能与我尝试反对的网站相关联,但我在任何网站上都遇到了相同的错误 - 请参阅下面的snippit,其中我已将 presence_of_element_located 替换为 visibility_of_element_located 我正在尝试确认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()

完整堆栈跟踪如下所示,任何帮助将不胜感激!

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

更新 - &gt;在下面的几条评论之后,我对版本和浏览器进行了一些测试,这个问题似乎与Python 3和Firefox分离,该脚本适用于Python 2.7,适用于Chrome webdriver的两个版本的python。

2 个答案:

答案 0 :(得分:0)

复制粘贴相同的代码,它的工作原理。 Dint有足够的回购来发表评论,所以不得不把它放在答案部分。

答案 1 :(得分:0)

这些小改动对我有用。

  1. visibility_of_element_located ===&gt; presence_of_element_located
  2. driver.quit()===&gt; driver.close()
  3. 请参阅以下内容:

    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()