Selenium 如何使用动态列表的xpath单击它。元素也在相应地变化
xpath元素如下所示。。这些都是动态的,所以有时候,只有一个会在那里。。在某时三个人都会在那里。。。所以脚本必须识别出现的元素数量,并相应地逐个单击Selenium 如何使用动态列表的xpath单击它。元素也在相应地变化,selenium,selenium-webdriver,Selenium,Selenium Webdriver,xpath元素如下所示。。这些都是动态的,所以有时候,只有一个会在那里。。在某时三个人都会在那里。。。所以脚本必须识别出现的元素数量,并相应地逐个单击 public void NumberOfStops(){ Util.pause(Util.WaitingTime.SHORT); JavascriptExecutor jse = (JavascriptExecutor)getDriver(); jse.executeScript("window.scrollBy(0,10
public void NumberOfStops(){
Util.pause(Util.WaitingTime.SHORT);
JavascriptExecutor jse = (JavascriptExecutor)getDriver();
jse.executeScript("window.scrollBy(0,1000)", "");
Util.pause(Util.WaitingTime.SHORT);
Util.pause(Util.WaitingTime.SHORT);
List<WebElement> AirlineStops = this.getDriver().findElements(By.cssSelector("ng-scope layout-row"));
System.out.println("airlineSize = " + Integer.toString(AirlineStops.size()));
for ( WebElement stops22 : AirlineStops ) {
Util.pause(Util.WaitingTime.SHORT);
stops22.click();
Util.pause(Util.WaitingTime.SHORT);
}
正如您所看到的,如果元素的数量发生变化,那么特定的div就会发生变化 包含所有变化项的通用xpath将是
.//*[@id='stops']/div/check-box-switch/div/
所以你可以做以下事情
WebElement baseItem= this.getDriver().findElements(By.xpath(".//*[@id='stops']/div/check-box-switch/div"));
List<WebElement> allItems = baseItem.findElements(By.xpath("/div"));
for(WebElement item : allItem){
WebElement currentItem = item;
//do your action here
}
WebElement baseItem=this.getDriver().findElements(By.xpath(“./*[@id='stops']]/div/复选框开关/div”);
List allItems=baseItem.findElements(By.xpath(“/div”);
对于(WebElement项:allItem){
WebElement currentItem=项目;
//你在这里做什么
}
谢谢Kushal,这里的“我”是什么?再次感谢。。它起作用了。。但它只从3个元素中识别出一个元素。。任何猜测??System.out.println(“airlineSize=“+Integer.toString(allItems.size()));以上代码仅打印1而不是3。。
WebElement baseItem= this.getDriver().findElements(By.xpath(".//*[@id='stops']/div/check-box-switch/div"));
List<WebElement> allItems = baseItem.findElements(By.xpath("/div"));
for(WebElement item : allItem){
WebElement currentItem = item;
//do your action here
}