导出到Excel按钮

时间:2016-11-22 20:48:45

标签: python selenium

我正在尝试从网站下载数据,虽然该网站根据我的搜索提供服务,但导出到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
from selenium.webdriver.common.by import By

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((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\Maggie\Desktop\Files\R Files")
driver = webdriver.Chrome(chrome_options=options)

driver.close()

错误:

Page is ready!

Traceback (most recent call last):
  File "C:\Users\Maggie\Desktop\Files\websitescraping.py", line 22, in <module>
    search_button.click()
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py", line 77, in click
    self._execute(Command.CLICK_ELEMENT)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py", line 494, in _execute
return self._parent.execute(command, params)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 236, in execute
    self.error_handler.check_response(response)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 192, in    check_response
    raise exception_class(message, screen, stacktrace)
ElementNotVisibleException: Message: element not visible
  (Session info: chrome=54.0.2840.71)
  (Driver info: chromedriver=2.24.417431   (9aea000394714d2fbb20850021f6204f2256b9cf),platform=Windows NT 6.1.7601 SP1 x86_64)

1 个答案:

答案 0 :(得分:0)

学习者,这应该考虑你的问题。需要更改为EC.visibility,而选项代码段会在以后给你错误,所以也要检查出来。

from selenium.webdriver.chrome.options import Options

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.visibility_of_element_located((By.ID,"cplMain_btnExportToExcel")))
    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 = webdriver.ChromeOptions()
options.add_argument("download.default_directory=C:\Users\Maggie\Desktop\Files\R Files")
driver = webdriver.Chrome(r"C:\Python27\Scripts\chromedriver.exe", chrome_options = options)