How to select particular location from the drop do

2020-05-07 19:37发布

问题:

Problem Description:

I am trying to automate the filling of the form in which there's a locality dropdown list. I am facing problem in selecting the option which are suggest in the suggestion list. The options in the suggestions list are provided by an api response i.e its dynamic. I am not able to select one of the option from the suggested list.

String Locality ="//label[contains(text(),'Locality')]/following-sibling::input";

public void insertData(DataTable str) throws InterruptedException {
    List<List<String>> list = str.asLists(String.class);
    super.identifyElement(locators.xpath, Locality), list.get(1).get(5));// value sendkey= Akurdi;

HTML Element:

<label _ngcontent-c4="" for="Location" class="active">Locality</label>
<input _ngcontent-c4="" autocapitalize="off" autocorrect="off" class="mb-0 ng-valid ng-dirty ng-touched" formcontrolname="locality" placeholder="" spellcheck="off" stype="locality" type="text" ng-reflect-klass="mb-0" ng-reflect-ng-class="[object Object]" ng-reflect-name="locality" ng-reflect-model="Akurdi" autocomplete="off">

Sendkey value form cucumber screnario: Akurdi... Display Input dropdown list UI

回答1:

In these cases I am working with these drop-downs as with usual List. Clicking on drop-down opening, waiting for drop-down to appear, then iterating in the list of it's elements, searching what I need by name, then clicking on it.



回答2:

Solution

        WebDriver driver = new ChromeDriver();

        driver.get("http://connexistech.net/godamwale/warehouse");

        WebElement locality = driver.findElement(By.xpath(".//div[@id='location']//input")); 

        locality.sendKeys("Akrudi");
       //Here at the end [1] can be any item from 1 to 5 since its showing only 5 suggestions all the time
        String xp = "//div[contains(@class, 'pac-container')]//div[contains(@class, 'pac-item')][1]";

        WebDriverWait wait = new WebDriverWait(driver, 10);
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//div[contains(@class, 'pac-container')]/div[contains(@class, 'pac-item')]")));
        WebElement element = driver.findElement(By.xpath(xp));             

        wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("xp")));
        element.click();