繁体   English   中英

使用 Selenium 执行自动建议

[英]To perform Autosuggestion using Selenium

使用自动建议从下拉列表中选择选项时遇到一些问题。 请给出解决方案以选择该选项。

相关代码发布如下:-

@Test(priority = 4)
public void ReportType() throws InterruptedException {
 WebElement reporttype = driver.findElement(By.xpath("html/body/form/div[3]/table[2]/tbody/tr[3]/td/table/tbody/tr[1]/td/table/tbody/tr[2]/td/div/span/table/tbody/tr[3]/td[2]/span/table/tbody/tr/td[2]/input[1]"));
 reporttype.clear();
 reporttype.sendKeys("NMQ De");
 WebDriverWait wait = new WebDriverWait(driver, 10);
 wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("html/body/form/div[3]/table[2]/tbody/tr[3]/td/table/tbody/tr[1]/td/table/tbody/tr[2]/td/div/span/table/tbody/tr[3]/td[2]/span/div/ul/li[4]")));
 Thread.sleep(5000);
 driver.findElement(By.xpath("html/body/form/div[3]/table[2]/tbody/tr[3]/td/table/tbody/tr[1]/td/table/tbody/tr[2]/td/div/span/table/tbody/tr[3]/td[2]/span/div/ul/li[4]")).click();
}

当你做一些谷歌搜索时,把你的问题当作谷歌搜索谷歌为你提供自动建议

public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
String textToSelect = "headlines today";

WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.co.in/");
Thread.sleep(2000);
WebElement autoOptions= driver.findElement(By.id("lst-ib"));
autoOptions.sendKeys("he");

List<WebElement> optionsToSelect = driver.findElements(By.xpath("//div[@class='sbqs_c']"));

for(WebElement option : optionsToSelect){
    System.out.println(option);
    if(option.getText().equals(textToSelect)) {
        System.out.println("Trying to select: "+textToSelect);
        option.click();
        break;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM