Selenium WebDriver从下拉列表中自动填充值

时间:2017-02-15 09:43:15

标签: selenium xpath auto-populate

你能帮我找一个webdriver中的元素:

  1. 假设我们有两个下拉菜单,一个是客户端和第二个设施。此外,在没有选择客户的情况下,我们无法选择设施作为其禁用。
  2. 我们从下拉菜单中选择了客户价值。
  3. 现在我为新标签编写了一个脚本。
  4. 之后,我要通过ID查找设施字段,但是显示找不到元素,那么请你帮我一下吗? .. 随附的屏幕截图供您参考。enter image description here 你能查一下吗?

1 个答案:

答案 0 :(得分:0)

继续上一个问题Select options from Autopopulate text boxes using Selenium webdriver

当您第二次使用相同的cssSelector时,它会找到当时不可见的第一个下拉元素。您需要使用标签文本使用更具体的定位器: -

WebElement facility = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("facility")))
facility.sendKeys("Ho")

List<WebElement> facilityOptions = wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath(".//div[label[text() = 'Ordering Location']]/ul[@class = 'typeahead dropdown-menu']//a")))
facilityOptions.get(0).click()

完整的工作示例代码: -

driver.get("https://bioceptbetaweb.azurewebsites.net/Account/Login");
driver.manage().window().maximize()

WebDriverWait wait = new WebDriverWait(driver, 60)

wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("username"))).sendKeys("ajay.kumar@technossus.com");
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("password"))).sendKeys("Ajay@123");
wait.until(ExpectedConditions.elementToBeClickable(By.id("btn-Login"))).click();
wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Place a New Order"))).click();

wait.until(ExpectedConditions.invisibilityOfElementLocated(By.id("loaderDiv")));


//this sleep is required because after invisibility of loader focus goes to first input which is Requisition Number
//If you are filling form from first input no need to for this sleep
//if you want to input directly to client field need to sleep to avoid focus first      
Thread.sleep(3000);


WebElement client = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("client")));
client.sendKeys("Ho");

List<WebElement> dropdownOptions = wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath(".//div[label[text() = 'Client']]/ul[@class = 'typeahead dropdown-menu']//a")));
dropdownOptions.get(0).click();

WebElement facility = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("facility")));
facility.sendKeys("Ho");

List<WebElement> facilityOptions = wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath(".//div[label[text() = 'Ordering Location']]/ul[@class = 'typeahead dropdown-menu']//a")));
facilityOptions.get(0).click();