[英]How to select particular location from the drop down list which is initiated dynamically using api response in selenium webdriver?
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 ie its dynamic. 建议列表中的选项由api响应(即动态)提供。 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: HTML元素:
<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 Sendkey值形式,黄瓜脚本:Akurdi ... 显示输入下拉列表UI
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. 单击下拉菜单,等待下拉菜单出现,然后在其元素列表中进行迭代,按名称搜索我需要的内容,然后单击它。
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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.