运行OS X和Python 3.5.1。
简单地想知道WebDriverWait搜索xpath的正确上下文是什么,然后在加载包含的网页后继续点击它。
WebDriverWait(driver, 10000).until(EC.presence_of_element_located(driver.find_element_by_xpath(".//*[@id='maincontent']/section[2]/div[1]/table/tbody/tr[1]/td[7]/button")))
a = driver.find_element_by_xpath(".//*[@id='maincontent']/section[2]/div[1]/table/tbody/tr[1]/td[7]/button")
a.click()
错误代码如下
Traceback (most recent call last):
File "/Users/hansh/Desktop/markets.py", line 22, in <module>
WebDriverWait(driver, 10000).until(EC.presence_of_element_located(driver.find_element_by_xpath(".//*[@id='maincontent']/section[2]/div[1]/table/tbody/tr[1]/td[7]/button")))
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 258, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 712, in find_element
{'using': by, 'value': value})['value']
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 201, in execute
self.error_handler.check_response(response)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/remote/errorhandler.py", line 181, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: {"method":"xpath","selector":".//*[@id='maincontent']/section[2]/div[1]/table/tbody/tr[1]/td[7]/button"}
Stacktrace:
at FirefoxDriver.prototype.findElementInternal_ (file:///var/folders/b3/h2pq8lmx4p11tb5h3m49xbth0000gn/T/tmpb9uhnk6f/extensions/fxdriver@googlecode.com/components/driver-component.js:10659)
at FirefoxDriver.prototype.findElement (file:///var/folders/b3/h2pq8lmx4p11tb5h3m49xbth0000gn/T/tmpb9uhnk6f/extensions/fxdriver@googlecode.com/components/driver-component.js:10668)
at DelayedCommand.prototype.executeInternal_/h (file:///var/folders/b3/h2pq8lmx4p11tb5h3m49xbth0000gn/T/tmpb9uhnk6f/extensions/fxdriver@googlecode.com/components/command-processor.js:12534)
at DelayedCommand.prototype.executeInternal_ (file:///var/folders/b3/h2pq8lmx4p11tb5h3m49xbth0000gn/T/tmpb9uhnk6f/extensions/fxdriver@googlecode.com/components/command-processor.js:12539)
at DelayedCommand.prototype.execute/< (file:///var/folders/b3/h2pq8lmx4p11tb5h3m49xbth0000gn/T/tmpb9uhnk6f/extensions/fxdriver@googlecode.com/components/command-processor.js:12481)
答案 0 :(得分:0)
根据documentation,正确的语法是元组,其中第一个元素是定位器类型,第二个元素是定位器值。用这种方式重写:
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(driver, 10)
presence_of = EC.presence_of_element_located
wait.until(presence_of((By.XPATH, ".//*[@id='maincontent']/section[2]/div[1]/table/tbody/tr[1]/td[7]/button")))
另请注意,传递到WebDriverWait
构造函数的超时值是,以秒为单位,而不是毫秒。您可能意味着10
而不是10000
。