2016-11-22 41 views
0

我可以使用下面的代碼過濾數據,但導出到Excel不起作用。我請求你如何改進我的代碼片段以指導Python等待數據完全加載,然後將excel文件下載到所需的文件夾。如何判斷Python等待Selenium?

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 

driver = webdriver.Chrome("C:\Python27\Scripts\chromedriver.exe") 
driver.get("https://etrakit.friscotexas.gov/Search/permit.aspx") 

number_option = driver.find_element_by_id("cplMain_btnSearch") 
number_option.click() 

delay = 3 
try: 
    WebDriverWait(driver, delay).until(EC.presence_of_element_located(driver.find_element_by_id("cplMain_btnSearch"))) 
    print "Page is ready!" 
except TimeoutException: 
    print "Loading took too much time!" 


search_button = driver.find_element_by_id("cplMain_btnExportToExcel") 
search_button.click() 

options.add_argument("download.default_directory=C:\Users\Patrick\Desktop\Programming\R Files") 
driver = webdriver.Chrome(chrome_options=options) 

driver.close() 

的錯誤:

Traceback (most recent call last): 
    File "C:\Users\Patrick\Desktop\Programming\aspxscraping.py", line 14, in <module> 
    WebDriverWait(driver, delay).until(EC.presence_of_element_located(driver.find_element_by_id("cplMain_btnSearch"))) 

File "C:\Python27\lib\site-packages\selenium\webdriver\support\wait.py", line 71, in until 
value = method(self._driver) 
    File "C:\Python27\lib\site-packages\selenium\webdriver\support\expected_conditions.py", line 63, in __call__ 
return _find_element(driver, self.locator) 
    File "C:\Python27\lib\site-packages\selenium\webdriver\support\expected_conditions.py", line 328, in _find_element 
return driver.find_element(*by) 
TypeError: find_element() argument after * must be a sequence, not WebElement 

回答

1

嘗試更換此

WebDriverWait(driver, delay).until(EC.presence_of_element_located(driver.find_element_by_id("cplMain_btnSearch")) 

與此

from selenium.webdriver.common.by import By 

WebDriverWait(driver, delay).until(EC.presence_of_element_located((By.ID, "cplMain_btnSearch"))) 
+0

感謝安德森,對於代碼工作。現在,它顯示了Search.Button 回溯一個錯誤(最新最後調用): 文件 「C:\用戶\ aqureshi \桌面\程序\ aspxscraping.py」 22行,在 search_button.click() –

+0

將完整的例外日誌添加到您的問題和用於搜索按鈕的「HTML」代碼 – Andersson

1

你的問題是在這裏:

EC.presence_of_element_located(driver.find_element_by_id("cplMain_btnSearch")) 

等待的一點是不要自己去尋找元素,因爲它可能不在那裏!恰巧你在這種情況下發現它,錯誤是在抱怨你使用了presence_of_element_located。這裏有您需要做什麼:

EC.presence_of_element_located((By.ID, "cplMain_btnSearch")) 

請參閱該文檔:http://selenium-python.readthedocs.io/waits.html#explicit-waits

相關問題