1+ import time
2+
3+ from selenium import webdriver
4+ from selenium .webdriver .chrome .service import Service
5+ from selenium .webdriver .common .by import By
6+ from selenium .webdriver .support .select import Select
7+
8+ options = webdriver .ChromeOptions ()
9+ options .add_experimental_option ("detach" , True )
10+
11+ ser_obj = Service ("C:\Drivers\chromedriver_win32\chromedriver.exe" )
12+ driver = webdriver .Chrome (options = options , service = ser_obj )
13+ driver .get ('https://testautomationpractice.blogspot.com/' )
14+
15+ # dropdown has tag name 'select'. and it is a single web element
16+ # inside the select tag we have multiple tag names called 'options' which hold text or values
17+
18+ # first identify the dropdown and store it
19+ dropdown_element = driver .find_element (By .XPATH , "//select[@id='country']" )
20+ # now we can't access the option directly in a dropdown so, we have to use the built_in select module in a package
21+ # we will pass dropdown element to the select module in order to access its options.
22+
23+ # option_element = Select(driver.find_element(By.XPATH, "//select[@id='country']")) # we can do this as well
24+ # Note: select method is only for the 'select' tag.sometimes we have 'div' or some other tag for that we will use XPATH
25+
26+ opt_element = Select (dropdown_element ) # object of select class (use it for accessing option elements)
27+ opt_element .select_by_visible_text ('Canada' ) # case Sensitive: provide exactly the same option text otherwise error
28+ opt_element .select_by_value ('usa' ) # extra the value attribute value from HTML DOM and select the option via it
29+ opt_element .select_by_index (3 ) # select the value using index.you have to find the index manually.don't write it in db.commas as it is index number
30+
31+ # capture all the options and catch them. we have 2 approaches for that
32+ # 1: write a common XPATH and capture option
33+ # 2: use built in methode 'option' and capture all the option inside the dropdown
34+ # you can use child::option , or you can just use /option and, it will select all the option
35+ # capture_opt_xp = driver.find_elements(By.XPATH, "//select[@id='country']/child::option")
36+ capture_opt_xp = driver .find_elements (By .XPATH , "//select[@id='country']/option" )
37+
38+ for cop in capture_opt_xp :
39+ print (cop .text )
40+
41+ builtin_op = opt_element .options # this will return all the option elements inside the dropdown
42+ for cop in builtin_op :
43+ print (cop .text )
44+
45+ # time.sleep(3)
46+ # if I want to print a specific option without using builtin function ie 'select_by_value' or select_by_index etc ...
47+ for cop in capture_opt_xp :
48+ if cop .text == 'France' :
49+ cop .click ()
50+ break # once the element is found then there is no need to look for other elements so 'break' it
51+
52+ # suppose instead of select tag there is div or button or some other tag.how to find option inside the dropdown
53+ # we will write a Xpath for that and then we can store all the element and do whatever we want
54+
55+ opt_len = driver .find_elements (By .XPATH , "//*[@id='country']/option" )
56+ print ('total number of option are :' , len (opt_len ))
0 commit comments