1

I have a form which has two drop-down menu say A and B. Contents of drop-down B is dependent on the selection of drop-down A.

The form uses AJAX to load the contents of drop down B.

I am using selenium and python to automatically select the drop down. I am able to select the drop down A but due to the use of AJAX my code is not working for selecting the content of drop-down B.

I have searched the selenium documentation (Explicit wait) and some stackoverflow answers but still I am unable to implement it in python. I am a newbie in python and selenium so please bear me.

Here is a small portion of my code :

#District selection DROP-DOWN A
district=Select(driver.find_element_by_id("ddlDistrict85"))
district.select_by_value("1")

#SRO selection DROP-DOWN B
# I Need EXPLICIT WAIT logic here to wait till the entire drop-down B is loaded
sro=Select(driver.find_element_by_id("ddlSRO85"))
sro.select_by_value("1")

Suggest some logic to wait till entire drop-down B is loaded.

3 Answers 3

0

You can use the options attribute from Select to check you have elements in the dropdown

district=Select(driver.find_element_by_id("ddlDistrict85"))
district.select_by_value("1")

sro=Select(driver.find_element_by_id("ddlSRO85"))
while len(sro.options) == 0:
    continue
sro.select_by_value("1")
Sign up to request clarification or add additional context in comments.

4 Comments

@sakyan Can you check the size of sro.options before and after the dropdown is filled? maybe there is some default values and the condition in the while should be while len(sro.options) == 1 or more.
before loading the size of sro.options is 1. I tried by modifying your code but still it's not working.
@sakyan Care to share? :)
I got a solution, I am using time.sleep(delay) to wait the page to load the drop-down B.
0

You haven't shared what the html looks like for these different menus, so let me assume that drop-down B is wrapped by a DIV with a specific class, or even better an ID, perhaps:

<div id="menuB"> ... </div>

Now, you could use Expected Conditions to wait for that menu to appear.

from selenium.webdriver.support import expected_conditions as EC

wait = WebDriverWait(driver, 10)
element = wait.until(EC.visibility_of_element_located((By.ID,'menuB')))

Comments

0

After searching a lot I found a simple solution where I am able to select the contents of drop-down B. So I am answering my own question.

Use sleep function from time module to pause the execution of the program for some time(in seconds).

The program code would go like this :

import time #To import time module
#District selection
district=Select(driver.find_element_by_id("ddlDistrict85"))
district.select_by_value("1")
#SRO selection
time.sleep(5) 
sro=Select(driver.find_element_by_id("ddlSRO85"))
sro.select_by_value("1")

It's working now.

2 Comments

As a newbie, you will eventually discover that using hard coded sleeps is not reliable or robust, thus the availability of the WebDriverWait class. I encourage you to go learn more about it, and experiment further with ExpectedConditions. You will be much happier going forward!
Thanks @BreaksSoftware, I will surely try to explore more.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.