I have drop down option HTML code before select the drop down.
<div class="form-group">
<label class="col-md-3 control-label required-field" for="Position-GroupId">Search item</label>
<div class="col-md-6">
<div class="fs-wrap" tabindex="0"><div class="fs-label-wrap"><div class="fs-label">- Select -</div><span class="fs-arrow"></span></div><div class="fs-dropdown hidden"><div class="fs-search"><input type="search" placeholder="Search Fruit"></div><div class="fs-no-results hidden">No results found</div><div class="fs-options"><div class="fs-option selected g0" data-value="" data-index="0"><span class="fs-checkbox"><em></em></span><div class="fs-option-label">- Select -</div></div><div class="fs-option g0" data-value="22" data-index="1"><span class="fs-checkbox"><em></em></span><div class="fs-option-label">BANANA</div></div><div class="fs-option g0" data-value="84" data-index="2"><span class="fs-checkbox"><em></em></span><div class="fs-option-label">ORANGE</div>
After selecting the drop down the HTML Code as below:
Search Fruits
– Select –
No results found– Select –BANANAORANGE
I have used below code:
# 1. Wait for the dropdown to be clickable (Important!)
dropdown = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.XPATH, "//label[contains(text(),'Fruits')]/following-sibling::div/div/div/div[@class ='fs-label']")) # More Specific Xpath
)
# Click the dropdown to open it
dropdown.click()
Now drop down box opened.
Now I need to click ORANGE OPTION:
For that I have used below code:
# 3. Wait for the options to be visible (Important!)
orange_option = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.XPATH, "//label[contains(text(),'Fruits')]/following-sibling::div/div/div[2]/div[3]/div[2]/span/following-sibling::div[contains(text(),'ORANGE')]"))
)
# 4. Click the "ORANGE" option
orange_option.click()
# Verification (Optional but highly recommended)
selected_value = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, "//label[contains(text(),'Fruits')]/following-sibling::div/div/div/div"))
)
assert selected_value.text == "ORANGE", f"Expected ORANGE, but got {selected_value.text}"
print("Orange selected Successfully")
I have hard coded the following XPATH to select each items :
//label[contains(text(),'Fruits')]/following-sibling::div/div/div[2]/div[3]/div[1]/span/following-sibling::div[contains(text(),'-Select-')]
//label[contains(text(),'Fruits')]/following-sibling::div/div/div[2]/div[3]/div[2]/span/following-sibling::div[contains(text(),'BANANA')]
//label[contains(text(),'Fruits')]/following-sibling::div/div/div[2]/div[3]/div[3]/span/following-sibling::div[contains(text(),'ORANGE')]
//label[contains(text(),'Fruits')]/following-sibling::div/div/div[2]/div[3]/div[4]/span/following-sibling::div[contains(text(),'APPLE')]
//label[contains(text(),'Fruits')]/following-sibling::div/div/div[2]/div[3]/div[5]/span/following-sibling::div[contains(text(),'LEMON')]
Now how to change my hardcoded XPATH into dynamic one?
Example
Fruits = [BANANA,ORANGE,APPLE,LEMON]
random_fruits_option = random.choice(Fruits)
HTML:
While each run the fruit name should randomly select in drop down using selenium 4?
5