【问题标题】:Handle Ajax Auto Suggest Drop List using selenium使用 selenium 处理 Ajax 自动建议删除列表
【发布时间】:2018-12-26 23:19:08
【问题描述】:

我想使用以下代码在http://busindia.com/ 的 From 字段中输入值

driver.findElement(By.id("matchFromPlace")).sendKeys("Udupi");

在 from 输入中键入 udupi 时,会出现一个选项列表,我们可以从中选择 udupi

【问题讨论】:

  • 我无法选择自动建议下拉列表的 xpath,我使用的是谷歌浏览器

标签: java selenium-webdriver


【解决方案1】:

请尝试以下代码:

Actions action = new Actions(driver);
action.sendKeys(Keys.DOWN);
action.sendKeys(Keys.ENTER);
action.perform()

【讨论】:

    【解决方案2】:

    以下代码对我有用:

        Thread.sleep(2000);
    
        List<WebElement> OptionList = driver.findElements(By.xpath("//ul[contains(@class,'ui-autocomplete')]/li/a"));
        if(OptionList.size()>=1){
            for(int i=0;i<OptionList.size();i++)
            {
                String CurrentOption = OptionList.get(i).getText();
                if(CurrentOption.equalsIgnoreCase("UDUPI")){
                    System.out.println("Found the city : "+CurrentOption);
                    OptionList.get(i).click();
                }
            }
        }
        else{
            System.out.println("OptionList is empty");
        }
    

    【讨论】:

    • 谢谢它正在工作。你能告诉我你是如何得到 xpath //ul[contains(@class,'ui-autocomplete')]/li/a 的,因为我无法选择下拉菜单来检查它。我正在使用 chrome 驱动程序
    • 即使我使用的是 chrome 驱动程序。我可以右键单击下拉菜单来检查它。 “@class,'ui-autocomplete''” - 这是在互联网上的大多数动态下拉列表中观察到的。如果您喜欢,请将答案标记为已接受! :)
    【解决方案3】:
    public class Ajaxcontrol {
    
    public WebDriver driver;
    
    
    @Test()
    
    public void f () throws Exception { 
    
        driver.findElement(By.xpath("//input[@title='Search']")).sendKeys("selenium");
        Thread.sleep(3000);
        driver.findElement(By.xpath("//input[@name='btnK']")).click();
        Thread.sleep(3000);
        driver.findElement(By.xpath("//input[@title='Search']")).click();
        Thread.sleep(3000);
        String str=driver.findElement(By.xpath("//ul[@class='erkvQe']")).getText();
    
                System.out.println(str);
    
                 String[] s= str.split("\n");
                    System.out.println(s.length);
    
                            for(int i=0;i<s.length;i++)
    {
    
                                    if(s[i].equalsIgnoreCase("selenium tutorial"))
             {
               driver.findElement(By.xpath("//input[@title='Search']")).clear();
               driver.findElement(By.xpath("//input[@title='Search']")).sendKeys(s[i]);
               Thread.sleep(5000);
    
               driver.findElement(By.xpath("//button[@type='button']")).click();
               Thread.sleep(5000);
               driver.findElement(By.xpath("//h3[contains(text(),'Selenium Tutorial for Beginners: Learn WebDriver i')]")).click();
             }
    }
    
    
    }
    
    
    @BeforeTest
    
    public void beforeTest() {
    
        //System.setProperty("webdriver.chrome.driver","C:\\Selenium DriverFiles\\chromedriver.exe");
        //System.setProperty("webdriver.gecko.driver","C:\\Selenium DriverFiles\\geckodriver-v0.23.0-win32\\geckodriver.exe");
        System.setProperty("webdriver.ie.driver","C:\\Selenium DriverFiles\\IEDriverServer.exe");
        driver = new InternetExplorerDriver();
        //driver = new ChromeDriver();
        //driver = new FirefoxDriver();
        driver.get("https://google.com");
        driver.manage().window().maximize();
    
    }
    
    
    @AfterTest
    
    public void afterTest() {
    
        driver.quit();
    }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-10
      • 2016-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-03
      • 2023-03-24
      相关资源
      最近更新 更多