1

I have a scenario in which i am finding bit difficult to automate . The test scenario is : There are two drop down . 1.To select the state. 2.To select the respective cities.

So i need to write a script to automate , where i need to select a state from State drop down and for that state i need to select all the cities one after the other and once all the cities selection is done for that respective state ,i need to come out of that loop and select a different state and repeat this process .

This is the code which i have written .

    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 have not given break statement ,it will through an error after completing entire iteration . But if i give a break it wont work the way i want.

This is the HTML code of the drop down. For 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 i select the state then the city drop down will appear.

  • Can you share the code of dropdown? – Alexey R. Oct 30 '18 at 15:17
  • Why do you need to select all cities for each state in turn? If your goal is to verify that the correct cities for each state are loaded it's possible there is a different method depending on how the web page is coded. Without knowing the reason you need the test and the web page code, it's not possible to give you a good answer – Kate Paulk Oct 30 '18 at 17:38
  • Its a registration page , so i need to write a code where a user will be registered to each city @KatePaulk – manoj Oct 31 '18 at 6:15
  • @AlexeyR. this is the code for state drop down – manoj Oct 31 '18 at 12:14
  • i got the solution for my question thank you for all the help – manoj Nov 2 '18 at 4:58
0

Try the below code... To select the state,

WebElement elem = driver.findElement(By.Id("your locator value"));
Select stateoption = new Select(elem);
stateoption.SelectByVisibleText("");

Above code selects a specified "state" form the dropdown.Once the "state" is selected,the cities will be listed out .So for selecting the cities try the code,

List<WebElement> ele = driver.findElements(By.Xpath(""));
for(WebElement cities:elem) {
   if(!cities.isSelected()) cities.click();
}

Please comment out if you find any issues.

  • i require python code @shanila ,but still thank your for your answer – manoj Oct 31 '18 at 12:12
0

I got answer to my question, By using two for loop for both state and city drop down i was able get the result which i wanted .

   def test_state(self):                                                                                        
    element = self.get_element('autocomplete-state', 'id')                                                             
    for state in element.find_elements_by_tag_name('option'):                            
        state.click()                                                                                                               
        element = self.get_element('autocomplete-city', 'id')                                                                                       
        for city in element.find_elements_by_tag_name('option'):                                                                          
            city.click()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.