我正在尝试访问两个字段的值Year
以及Quarter
从此特定站点访问的值。在StackOverflow的一名成员的帮助下year
,现在,如果我想访问该quarter
部件,那么我便能够实现该部件的代码,那么我该如何访问该部件。
下面是到目前为止的实现。
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-error')
options.add_argument('--ignore-ssl-errors')
url = "https://lifeinsurance.adityabirlacapital.com/about-us/public-disclosure"
driver = webdriver.Chrome(executable_path='drivers/chromedriver.exe')
driver.get(url)
WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.CSS_SELECTOR, "div.selectize-input.items.full.has-options.has-items"))
).click()
year_dropdown = WebDriverWait(driver, 20).until(
EC.visibility_of_all_elements_located(
(By.CSS_SELECTOR, "div.selectize-dropdown-content div.option")
)
)
for year in year_dropdown:
print(year.text)
欢迎任何提示和建议。
您的年份和季度都有相同的结构。一个selectPublicYear
和另一个一起selectPublicQuarter
上课
wait = WebDriverWait(driver, 20)
# Years
year_selector = ".selectize-control.selectPublicYear"
year_option_selector = year_selector + " .option"
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, year_selector))).click()
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, year_option_selector)))
years = driver.find_elements(By.CSS_SELECTOR, year_option_selector)
for year in years:
print(year.text)
years[-1].click()
# Quarters
quarter_selector = ".selectize-control.selectPublicQuarter"
quarter_option_selector = quarter_selector + " .option"
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, quarter_selector))).click()
# Wait for option element to be clickable.
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, quarter_option_selector)))
# Get all option elements
quarters = driver.find_elements(By.CSS_SELECTOR, quarter_option_selector)
for quarter in quarters:
print(quarter.text)
# Select last one
quarters[-1].click()
您可以添加方法以按值选择下拉列表:
def select_dropdown(name, selector, value):
print(f'Select "{name}" dropdown, value: "{value}"')
# Selectors
selector = ".selectize-control" + selector
option_selector = selector + " .option"
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, selector))).click()
# Wait for option element to be clickable
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, option_selector)))
# Get all options elements
option_elements = driver.find_elements(By.CSS_SELECTOR, option_selector)
# Get text values from options
options = [opt.text.strip() for opt in option_elements]
print(f'Available values: {options}')
# Check if options have value
if value not in options:
raise Exception(f'"{name}" dropdown does not have "{value}" value. Available values: {options}')
option_elements[options.index(value)].click()
调用方式:
select_dropdown(name="Year", selector=".selectPublicYear", value="2020-2021")
select_dropdown(name="Quarter", selector=".selectPublicQuarter", value="Q2: Sep, 2020")
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句