2016-11-22 42 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

相关问题