简体   繁体   English

如何告诉Python等待Selenium?

[英]How to tell Python to wait for Selenium?

I am able to filter the data with the code below but the Export to Excel does not work. 我可以使用以下代码过滤数据,但无法导出到Excel。 I request your kind assistance on how to improve my snippet below to instruct Python to wait for the data to get fully loaded and then download the excel file to the desired folder. 我要求您提供有关如何改善以下代码段的帮助,以指示Python等待数据完全加载,然后将excel文件下载到所需的文件夹。

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

The Error: 错误:

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

Try to replace this 尝试取代这个

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

with this 有了这个

from selenium.webdriver.common.by import By

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

Your problem is here: 您的问题在这里:

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

The point of the wait is to not try finding the element yourself, because it might not be there yet! 等待的重点是不要尝试自己查找元素,因为它可能还不存在! It so happens that you found it in this case and the error is complaining about your use of presence_of_element_located . 碰巧您在这种情况下找到了它,并且该错误抱怨您使用了presence_of_element_located Here's what you need to do: 这是您需要做的:

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

See the docs: http://selenium-python.readthedocs.io/waits.html#explicit-waits 请参阅文档: http : //selenium-python.readthedocs.io/waits.html#explicit-waits

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM