python - 如何将列表中的项目传递到 xpath 以从网站下载文件?
问题描述
下面的代码几乎可以正常工作,完全感谢 Jason Cook!但是,当我遍历列表中的项目时,第二个项目总是会引发错误。第一项有效,但第二项无效。这是错误消息。
NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//select[@id='DatesDropDownList']/option[text()='UBPR Ratio -- Single Period']"}
(Session info: chrome=87.0.4280.88)
好像有些东西需要重新设置,我猜。上面提到的错误似乎来自这行代码。
driver.find_element_by_xpath(path+"/option[text()='"+select+"']").click()
或者,也许这行代码。
driver.find_element_by_xpath(path+"/option[text()='"+dates[j].text+"']").click()
这是所有的代码。
# browser = webdriver.Chrome("C:/Utility/chromedriver.exe")
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
# using Google Chrome, can use the browser of your choice
driver = webdriver.Chrome('C:/Utility/chromedriver.exe')
url = 'https://cdr.ffiec.gov/public/PWS/DownloadBulkData.aspx'
driver.get(url)
# Wait for available products to load, and then select value.
path = "//select[@id='ListBox1']"
products = WebDriverWait(driver, 10).until(
EC.presence_of_element_located(
(By.XPATH, path)
)
)
list = ['Call Reports -- Single Period',
'Call Reports -- Balance Sheet, Income Statement, Past Due -- Four Periods',
'UBPR Ratio -- Single Period']
# Using for loop
for item in list:
select = item
driver.find_element_by_xpath(path+"/option[text()='"+select+"']").click()
time.sleep(2) # seconds
#nWait for years to load. Get a list of the years. Select one of these as an example.
path = "//select[@id='DatesDropDownList']"
dropdown = WebDriverWait(driver, 20).until(
EC.presence_of_element_located(
(By.XPATH, path)
)
)
time.sleep(2) # seconds
for j in range(0, 5):
dates = driver.find_elements_by_xpath(path+'/option')
# an example, you can loop through dates
driver.find_element_by_xpath(path+"/option[text()='"+dates[j].text+"']").click()
submit_button = driver.find_element_by_id("Download_0")
submit_button.click()
解决方案
使用WebDriverWait
() 并等待element_to_be_clickable()
。使用 seleniumselect
方法选择项目
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
driver = webdriver.Chrome('C:/Utility/chromedriver.exe')
driver.get('https://cdr.ffiec.gov/public/PWS/DownloadBulkData.aspx')
listproduct = ['Call Reports -- Single Period',
'Call Reports -- Balance Sheet, Income Statement, Past Due -- Four Periods',
'UBPR Ratio -- Single Period']
for product in listproduct:
Weblistbox=WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.ID,"ListBox1")))
select=Select(Weblistbox)
select.select_by_visible_text(product)
print("Product {} is selected".format(product))
for year in range(0,5):
datedropdown = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "DatesDropDownList")))
select = Select(datedropdown)
select.select_by_index(year)
datedropdown = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "DatesDropDownList")))
select = Select(datedropdown)
print("DateOrYear {} is selected".format(select.first_selected_option.text))
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.ID,'Download_0'))).click()
print("================================")
您的控制台输出将是这样的。
Product Call Reports -- Single Period is selected
DateOrYear 09/30/2020 is selected
DateOrYear 06/30/2020 is selected
DateOrYear 03/31/2020 is selected
DateOrYear 12/31/2019 is selected
DateOrYear 09/30/2019 is selected
================================
Product Call Reports -- Balance Sheet, Income Statement, Past Due -- Four Periods is selected
DateOrYear 2020 is selected
DateOrYear 2019 is selected
DateOrYear 2018 is selected
DateOrYear 2017 is selected
DateOrYear 2016 is selected
================================
Product UBPR Ratio -- Single Period is selected
DateOrYear 09/30/2020 is selected
DateOrYear 06/30/2020 is selected
DateOrYear 03/31/2020 is selected
DateOrYear 12/31/2019 is selected
DateOrYear 09/30/2019 is selected
推荐阅读
- awk - 使用 awk 通过密钥文件从文件中获取值
- javascript - Opensea JS SDK createSellOrder 方法似乎不起作用或静默失败
- javascript - 使用 Reactjs 和 WebRTC 选择屏幕后,屏幕共享屏幕不显示
- typescript - 如何在 vue 3 组合 API 中更新(设置)道具值?
- python - Django 授权装饰器(例如:需要登录)如何工作?
- excel - Excel VBA 检查以前版本中的项目
- message-queue - 多个开发者如何使用同一个队列进行开发?
- next.js - 有没有办法使用 next.config.js 为下一个 js 中的所有本地图像设置前缀?
- python - 从字典调用的python中的模拟函数
- mongodb - 无法从 MongoDB 读取集合