2015-12-29 43 views
0

運行OS X和Python 3.5.1。WebDriverWait語法錯誤

簡單地想知道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/[email protected]/components/driver-component.js:10659) 
    at FirefoxDriver.prototype.findElement (file:///var/folders/b3/h2pq8lmx4p11tb5h3m49xbth0000gn/T/tmpb9uhnk6f/extensions/[email protected]/components/driver-component.js:10668) 
    at DelayedCommand.prototype.executeInternal_/h (file:///var/folders/b3/h2pq8lmx4p11tb5h3m49xbth0000gn/T/tmpb9uhnk6f/extensions/[email protected]/components/command-processor.js:12534) 
    at DelayedCommand.prototype.executeInternal_ (file:///var/folders/b3/h2pq8lmx4p11tb5h3m49xbth0000gn/T/tmpb9uhnk6f/extensions/[email protected]/components/command-processor.js:12539) 
    at DelayedCommand.prototype.execute/< (file:///var/folders/b3/h2pq8lmx4p11tb5h3m49xbth0000gn/T/tmpb9uhnk6f/extensions/[email protected]/components/command-processor.js:12481) 

回答

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