无法使用 selenium webdriver 从应用程序中第二个字段的自动填充值中选择值

下面的代码适用于应用程序中的第一个字段,而相同的代码不适用于第二个字段。如果我们只执行能够看到预期输出的第二个字段步骤。尝试了所有类型的等待。但无法解决。有人可以查看下面的代码并建议我做错了什么。


package com.qa.base;


import java.util.List;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;

import org.openqa.selenium.Keys;

import org.openqa.selenium.NoSuchElementException;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.support.ui.ExpectedConditions;

import org.openqa.selenium.support.ui.WebDriverWait;


public class Sample {


    public static WebDriver driver;


    public static void main(String[] args) throws InterruptedException {


        System.setProperty("webdriver.chrome.driver",

                "C:\\Users\\Avanthi\\Downloads\\chromedriver_win32\\chromedriver.exe");

        driver = new ChromeDriver();

        driver.manage().deleteAllCookies();

        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

        driver.manage().timeouts().pageLoadTimeout(20, TimeUnit.SECONDS);

        driver.manage().window().maximize();

        driver.get("https://www.abhibus.com/");


        // driver.findElement(By.id("source")).sendKeys("adi");

        driver.findElement(By.id("source")).sendKeys("Bang");


        WebDriverWait wait = new WebDriverWait(driver, 30);


        wait.until(ExpectedConditions

                .visibilityOfAllElementsLocatedBy(By.xpath("//li[@class='ui-menu-item']//parent::li")));


        List<WebElement> list = driver.findElements(By.xpath("//li[@class='ui-menu-item']//parent::li"));


        System.out.println("Auto Suggest List ::" + list.size());


        for (int i = 0; i < list.size(); i++) {

            System.out.println(list.get(i).getText());


            if (list.get(i).getText().equalsIgnoreCase("Bangalore Kempegowda Intl Airport")) {

                System.out.println(list.get(i).getText());

                list.get(i).click();

                break;

            }

千巷猫影
浏览 109回答 1
1回答

蛊毒传说

package com.qa.base;import java.util.List;import java.util.concurrent.TimeUnit;import org.openqa.selenium.By;import org.openqa.selenium.Keys;import org.openqa.selenium.NoSuchElementException;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebElement;import org.openqa.selenium.chrome.ChromeDriver;import org.openqa.selenium.support.ui.ExpectedConditions;import org.openqa.selenium.support.ui.WebDriverWait;public class Sample {&nbsp; &nbsp; public static WebDriver driver;&nbsp; &nbsp; public static void main(String[] args) throws InterruptedException {&nbsp; &nbsp; &nbsp; &nbsp; System.setProperty("webdriver.chrome.driver",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "C:\\Users\\Avanthi\\Downloads\\chromedriver_win32\\chromedriver.exe");&nbsp; &nbsp; &nbsp; &nbsp; driver = new ChromeDriver();&nbsp; &nbsp; &nbsp; &nbsp; driver.manage().deleteAllCookies();&nbsp; &nbsp; &nbsp; &nbsp; driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);&nbsp; &nbsp; &nbsp; &nbsp; driver.manage().timeouts().pageLoadTimeout(20, TimeUnit.SECONDS);&nbsp; &nbsp; &nbsp; &nbsp; driver.manage().window().maximize();&nbsp; &nbsp; &nbsp; &nbsp; driver.get("https://www.abhibus.com/");&nbsp; &nbsp; &nbsp; &nbsp; driver.findElement(By.id("source")).sendKeys("Bang");&nbsp; &nbsp; &nbsp; &nbsp; WebDriverWait wait = new WebDriverWait(driver, 30);&nbsp; &nbsp; &nbsp; &nbsp; wait.until(ExpectedConditions&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .visibilityOfAllElementsLocatedBy(By.xpath("//li[@class='ui-menu-item']//parent::li")));&nbsp; &nbsp; &nbsp; &nbsp; List<WebElement> list = driver.findElements(By.xpath("//li[@class='ui-menu-item']//parent::li"));&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Auto Suggest List ::" + list.size());&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < list.size(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(list.get(i).getText());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (list.get(i).getText().equalsIgnoreCase("Bangalore Kempegowda Intl Airport")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(list.get(i).getText());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list.get(i).click();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; driver.findElement(By.id("destination")).sendKeys("Chen");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WebDriverWait wait1 = new WebDriverWait(driver, 60);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; wait1.until(ExpectedConditions&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .visibilityOfAllElementsLocatedBy(By.xpath("//li[@class='ui-menu-item']//parent::ul//following::ul//li")));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<WebElement> goingtolist = driver.findElements(By.xpath("//li[@class='ui-menu-item']//parent::ul//following::ul//li"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Auto Suggest List ::" + goingtolist.size());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < goingtolist.size(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(goingtolist.get(i).getText());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (goingtolist.get(i).getText().equalsIgnoreCase("Chengannur Railway Station")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(goingtolist.get(i).getText());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; goingtolist.get(i).sendKeys(Keys.TAB);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } catch (NoSuchElementException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(e.getStackTrace());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(e.getMessage());&nbsp; &nbsp; &nbsp; &nbsp; } catch (Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(e.getStackTrace());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(e.getMessage());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java