Home > database >  How to click on dropdown by finding it's text or value using selenium in python?
How to click on dropdown by finding it's text or value using selenium in python?

Time:10-28

<div class="col-lg-6 col-sm-12 form-group drivers-form-group">
                        <label for="operating-system">Operating system</label>
                        <div class="select">
                            <select id="operating-system" class="w-100 form-control custom-select drivers-select">
                                        <option value="BIOSA">BIOS</option>
                                        <option value="NKLNA">NeoKylin</option>
                                        <option value="UBT18">Ubuntu® 18.04 LTS</option>
                                        <option value="LTSC1">Windows 10 64-Bit LTSC 2019</option>
                                        <option value="W10GE">Windows 10 CMIT Government Edition</option>
                                        <option value="WT64A" selected="">Windows 10, 64-bit</option>
                                        <option value="W2021">Windows 11</option>
                            </select>
                        </div>
                    </div>

This is a sample HTML text, The HTML is a dropdown and has 7 options.

Now with Basics of selenium, I can get the Xpath of the Windows 10 64 bit and can click on it, So that it changes the content as per Windows 10 64 bit. The xpath for windows 10 64 bit is //*[@id="operating-system"]/option[6]

2nd Url Sample

    <div class="col-lg-6 col-sm-12 form-group drivers-form-group">
                        <label for="operating-system">Operating system</label>
                        <div class="select">
                            <select id="operating-system" class="w-100 form-control custom-select drivers-select">
                                        <option value="BIOSA">BIOS</option>
                                        <option value="NKLNA">NeoKylin</option>
                                        <option value="WT64A" selected="">Windows 10, 64-bit</option>
                                        <option value="LTSC1">Windows 10 64-Bit LTSC 2019</option>
                                        <option value="W10GE">Windows 10 CMIT Government Edition</option>
                                        <option value="DE0GE">Windows 8 CMIT Government Edition</option>                 
                            </select>
                        </div>
                    </div>

Now in another sample Code provided below, Windows 10 64 Bit is in 3rd position,

So the xpath would be //*[@id="operating-system"]/option[3]

So if somehow in python I try to find

driver.find_element_by_xpath('//*[@id="operating-system"]/option[6]')

I would get data of Windows 10 64-bit for the first sample and Windows 8 CMIT for 2nd Sample

Now the only thing I want to ask is that is there any way where I select find Windows 10, 64-bit on the dropdown and click on it for every URL looped in through Selenium or any other library?

Also if we can try to select a value code that is the same for both sample data, Then it can work.

CodePudding user response:

As already informed, can make use of Select class to select an element from this drop-down

# Imports required:
from selenium.webdriver.support.select import Select

sel_option = Select(driver.find_element_by_id("operating-system"))

# Select by value
sel_option.select_by_value("LTSC1")

# Can also try with visible-text
sel_option.select_by_visible_text("Windows 10 64-Bit LTSC 2019")

Update: The Selected option as an attribute selected. Can use the same to extract text from the element.

selected_option = driver.find_element_by_xpath("//select[@id='operating-system']/option[@selected]")
print(selected_option.text)
Or
print(selected_option.get_attribute("innerText"))
  • Related