I am trying to access the values of both the fields Year
and Quarter
from this particular site. With the help from one member of StackOverflow, I was able to implement the code for the year
part, now if I want to access the quarter
part so how can I access the same.
Below is the implementation so far.
JavaScript
x
27
27
1
from selenium import webdriver
2
from selenium.webdriver.support.ui import WebDriverWait
3
from selenium.webdriver.common.by import By
4
from selenium.webdriver.support import expected_conditions as EC
5
6
options = webdriver.ChromeOptions()
7
options.add_argument('--ignore-certificate-error')
8
options.add_argument('--ignore-ssl-errors')
9
10
url = "https://lifeinsurance.adityabirlacapital.com/about-us/public-disclosure"
11
driver = webdriver.Chrome(executable_path='drivers/chromedriver.exe')
12
13
driver.get(url)
14
15
WebDriverWait(driver, 20).until(
16
EC.element_to_be_clickable((By.CSS_SELECTOR, "div.selectize-input.items.full.has-options.has-items"))
17
).click()
18
19
year_dropdown = WebDriverWait(driver, 20).until(
20
EC.visibility_of_all_elements_located(
21
(By.CSS_SELECTOR, "div.selectize-dropdown-content div.option")
22
)
23
)
24
25
for year in year_dropdown:
26
print(year.text)
27
Any hints and suggestions are welcome.
Advertisement
Answer
You have same structure for both years and quarters. One with selectPublicYear
and another with selectPublicQuarter
class
JavaScript
1
28
28
1
wait = WebDriverWait(driver, 20)
2
3
# Years
4
year_selector = ".selectize-control.selectPublicYear"
5
year_option_selector = year_selector + " .option"
6
7
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, year_selector))).click()
8
9
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, year_option_selector)))
10
years = driver.find_elements(By.CSS_SELECTOR, year_option_selector)
11
for year in years:
12
print(year.text)
13
years[-1].click()
14
15
# Quarters
16
quarter_selector = ".selectize-control.selectPublicQuarter"
17
quarter_option_selector = quarter_selector + " .option"
18
19
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, quarter_selector))).click()
20
# Wait for option element to be clickable.
21
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, quarter_option_selector)))
22
# Get all option elements
23
quarters = driver.find_elements(By.CSS_SELECTOR, quarter_option_selector)
24
for quarter in quarters:
25
print(quarter.text)
26
# Select last one
27
quarters[-1].click()
28
You can add method to select dropdown by value:
JavaScript
1
24
24
1
def select_dropdown(name, selector, value):
2
print(f'Select "{name}" dropdown, value: "{value}"')
3
4
# Selectors
5
selector = ".selectize-control" + selector
6
option_selector = selector + " .option"
7
8
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, selector))).click()
9
10
# Wait for option element to be clickable
11
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, option_selector)))
12
13
# Get all options elements
14
option_elements = driver.find_elements(By.CSS_SELECTOR, option_selector)
15
# Get text values from options
16
options = [opt.text.strip() for opt in option_elements]
17
print(f'Available values: {options}')
18
19
# Check if options have value
20
if value not in options:
21
raise Exception(f'"{name}" dropdown does not have "{value}" value. Available values: {options}')
22
23
option_elements[options.index(value)].click()
24
Call method:
JavaScript
1
3
1
select_dropdown(name="Year", selector=".selectPublicYear", value="2020-2021")
2
select_dropdown(name="Quarter", selector=".selectPublicQuarter", value="Q2: Sep, 2020")
3