Python-无法使用Selenium chromewebdriver下载文件-'失败-下载错误'

时间:2019-08-09 22:41:12

标签: python selenium selenium-webdriver xpath webdriverwait

我正尝试使用硒从谷歌浏览器下载文件。我在下面使用的代码工作正常。但是以某种方式它不再起作用了。有什么想法吗?

import os.path
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import Select

RAWDATA_URL = 'https://oui.doleta.gov/unemploy/DataDownloads.asp'
options = webdriver.ChromeOptions() 
prefs = {'download.default_directory' : SAVE_PATH, "download.prompt_for_download": False}
options.add_experimental_option('prefs', prefs)

driver = webdriver.Chrome(executable_path = DRIVE_PATH, chrome_options = options)


driver.get(RAWDATA_URL)
time.sleep(5)

下面的xpath只是从HTML复制而来的,应该是正确的

driver.find_element_by_xpath("//*[@id='main']/table[38]/tbody/tr[2]/td[5]/a").click()

我也尝试了get方法:

driver.get("https://oui.doleta.gov/unemploy/csv/ar9047.csv")

我期望csv文件可以成功下载。但是谷歌浏览器告诉我“失败-下载错误”。

更新:我简化了上面的问题。在我的项目中,实际上有两个步骤。首先从一个站点下载数据,然后导航到另一个站点以下载csv数据。

import datetime
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC



SUMMARY_URL = "https://oui.doleta.gov/unemploy/reemploy.asp"
RAWDATA_URL = 'https://oui.doleta.gov/unemploy/DataDownloads.asp'
REEMPLOYMENT_QTR = '09/30/2018' 

options = webdriver.ChromeOptions() 
prefs = {'download.default_directory' : SAVE_PATH, "download.prompt_for_download": False}
options.add_experimental_option('prefs', prefs)

driver = webdriver.Chrome(executable_path = DRIVE_PATH, chrome_options = options)

第一步:

driver.get(SUMMARY_URL)
time.sleep(5)

select = Select(driver.find_element_by_id('qtr'))
select.select_by_value(REEMPLOYMENT_QTR)
driver.find_element_by_xpath("//input[@name='submit'][@type='submit']").click()
re_table = driver.find_element_by_xpath("//*[@id='content']/table")

state = []
value = []
for re in re_table.find_elements_by_tag_name('tr'):
    c = 0 
    for ele in re.find_elements_by_tag_name('td'):
        if c == 0:
            state.append(ele.text.encode('utf8'))
            c += 1
        else:
            value.append(ele.text.encode('utf8'))


reemployment = pd.DataFrame({'state' : state, AS_OF_DATE : value})
reemployment = reemployment[['state', AS_OF_DATE]]

第二步(我的原始问题):

driver.execute_script("window.open('');") 
time.sleep(5)
driver.switch_to.window(driver.window_handles[1]) 
time.sleep(5)
driver.get(RAWDATA_URL)
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//th[text()='ETA 9047']//following::table[1]//tr/td/a[@title='Data']"))).click()

2 个答案:

答案 0 :(得分:0)

想必您正在尝试在 ETA 9047 部分中以文本为 Data 的元素上调用click(),并实现了必须诱使element_to_be_clickable()> WebDriverWait ,您可以使用以下Locator Strategy

  • 使用XPATH

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    chrome_options = webdriver.ChromeOptions() 
    chrome_options.add_argument("start-maximized")
    driver = webdriver.Chrome(options=chrome_options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
    driver.get("https://oui.doleta.gov/unemploy/DataDownloads.asp")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//th[text()='ETA 9047']//following::table[1]//tr/td/a[@title='Data']"))).click()
    
  • 浏览器快照:

9047

  

PS :确保您将 Selenium v3.141.59 ChromeDriver / Chrome < / em> v76.0

答案 1 :(得分:0)

我的问题是默认目录的保存路径存在问题: 它是“ C:/ Users / ...”,但应该是“ C:\ Users \ ...”,如下所示

    chrome_options = webdriver.ChromeOptions()
    prefs = {
    'download.default_directory': 'C:\\Users\\<username>\\Documents\\test\\',
    "download.prompt_for_download": False,
    "download.directory_upgrade": True,
    "safebrowsing_for_trusted_sources_enabled": False,
    "safebrowsing.enabled": False
    }
    chrome_options.add_experimental_option('prefs', prefs)