我有一个列表,每个部分下都有多个链接。每个部分都有不同的链接。我需要单击每个部分下的特定链接。我已经写了下面的代码,但当它执行时,在单击第一部分自动完成字段后,我无法单击自动完成字段的第二部分。这是我的代码。通过使用for-each,我无法选择第二个自动完成字段。请告诉我怎样才能走出这一步。
public class Autocomplete {
public static WebDriver driver;
public static void main(String[] args) throws InterruptedException {
ChromeOptions option = new ChromeOptions();
option.addArguments("--disable-notifications");
System.setProperty("webdriver.chrome.driver",".//src//browser//chromedriver.exe");
driver = new ChromeDriver(option);
driver.manage().window().maximize();
System.out.println("Browser Launch chrome");
driver.get("https://www.redbus.in/");
driver.findElement(By.xpath("//*[@id=\"src\"]")).sendKeys("ta");
AutocompleteRedbus.RebBus(By.xpath("//*[@id=\"src\"]"), "Tambaram, Chennai");
Thread.sleep(5000);
driver.findElement(By.xpath("//*[@id=\"dest\"]")).sendKeys("pon");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
AutocompleteRedbus.RebBus(By.xpath("//*[@id=\"dest\"]"), "Ponamaravathi");
}
}上面提到的AutocompleteRedbus调用方法代码如下:
public class AutocompleteRedbus extends Autocomplete{
public static void RebBus(By xpath , String text) throws InterruptedException {
List<WebElement> listOfLinks = driver.findElements(By.xpath("xpath"));
listOfLinks.forEach(link -> {
if (link.getText().equalsIgnoreCase("text")) {
link.click();
}
});
}
}发布于 2019-03-21 21:53:20
当您尝试从自动完成程序中选择值时,您使用的xpath不正确。请将xpath更改为我提供的xpath,它将正常工作。
AutocompleteRedbus.RebBus(By.xpath("//li[contains(@select-id,'results')]"), "Tambaram, Chennai");对于目标方法也是如此,如下所示:
AutocompleteRedbus.RebBus(By.xpath("//li[contains(@select-id,'results')]"), "Ponamaravathi");https://stackoverflow.com/questions/55280916
复制相似问题