selenium webdriver – To work with two different drop downs which are dependent on each other

I have a scenario where I find it a bit difficult to automate. The test scenario is: There are two types of dropdown. 1. To select the country. 2. To select the respective cities.

So I need to write an automation script, where I need to select a state from the state dropdown and for that state I need to select all cities one by one and once I have selected all the cities for that respective state, I need to exit that loop and choose a different state and repeat the process.

This is the code I wrote.

    def test_state(self):
    element = self.getAllTheElements(self.Register_page_elements,'xpath', 'option')
    for link in element:
        if link.get_attribute('value'):
            link.click()
            print("test")
            time.sleep(4)
            element = self.getAllTheElements('autocomplete-city', 'id', 'option')
            for link2 in element:
                if link2.get_attribute("value"):
                    link2.click()
                    print("state")
                    time.sleep(2)

Since I didn’t give a break statement, an error will occur after completing the entire iteration. But if I give it a break it won’t work the way I want it to.

This is the HTML code from the dropdown menu. for the state.

<div class="under-line">
                        <select type="text" class="enque-content" autocomplete="off" required="required" id="autocomplete-state" name="state" onchange="getCities(this)">
                            <option selected="" disabled="disabled" value="">Please select your state*</option>
                        <option value="1">Andaman and Nicobar Island</option><option value="2">Andhra Pradesh</option><label alt="First Name*" class="enque-placeholder-label" placeholder="First Name*"> </label><option value="3">Arunachal Pradesh</option><option value="4">Assam</option><option value="5">Bihar</option><option value="6">Chandigarh</option><option value="8">Dadra and Nagar Haveli</option><option value="7">Chhattisgarh</option><option value="9">Daman and Diu</option><option value="10">Delhi</option><option value="37">Goa</option><option value="12">Gujarat</option><option value="13">Haryana</option><option value="14">Himachal Pradesh</option><option value="15">Jammu and Kashmir</option><option value="16">Jharkhand</option><option value="17">Karnataka</option><option value="18">Kerala</option><option value="19">Lakshadweep</option><option value="20">Madhya Pradesh</option><option value="21">Maharashtra</option><option value="22">Manipur</option><option value="23">Meghalaya</option><option value="24">Mizoram</option><option value="25">Nagaland</option><option value="26">Odisha</option><option value="27">Puducherry</option><option value="28">Punjab</option><option value="29">Rajasthan</option><option value="30">Sikkim</option><option value="31">Tamil Nadu</option><option value="32">Telangana</option><option value="33">Tripura</option><option value="34">Uttar Pradesh</option><option value="35">Uttarakhand</option><option value="36">West Bengal</option></select>
                    </div>

Once you select the state, the city dropdown menu will appear.

Leave a Comment