简体   繁体   English

是否可以找到此元素的公共 xpath?

[英]Is it possible to find common xpath for this elements?

xpath of one element is: //div[@class=name-slider-header']//button//img[2]一个元素的xpath是: //div[@class=name-slider-header']//button//img[2]

xpath of another element is: //div[@class=name-slider-header']//button//img[1]另一个元素的xpath是: //div[@class=name-slider-header']//button//img[1]

Actually I need to check attribute of element must contains "red" after element gets disabled after clicking "n" times, so I am using element.getAttribute("src").contains("red");实际上,我需要在单击“n”次后元素被禁用后检查元素的属性必须包含“red”,所以我使用element.getAttribute("src").contains("red"); element2.getAttribute("src").contains("red");

Is it possible to find common xpath for this elements?是否可以找到此元素的公共 xpath?

The common XPath of these 2 elements is //div[@class=name-slider-header']//button//img .这 2 个元素的公共 XPath 是//div[@class=name-slider-header']//button//img So, you can get a list of elements and then iterate over the list extracting the element attribute, as following:因此,您可以获得一个元素列表,然后遍历提取元素属性的列表,如下所示:

elements = driver.findElements(By.xpath("//div[@class=name-slider-header']//button//img"));
for(WebElement element : elements){
    if(element.getAttribute("src").contains("red")){
      // do something
  }
}

Use the following xpath to identify the image elements where src value contains red使用下面的xpath来识别src值包含redimage元素

//div[@class='name-slider-header']//button//img[contains(@src, 'red')]

code:代码:

imgElements = driver.findElements(By.xpath("//div[@class='name-slider-header']//button//img[contains(@src, 'red')]"));

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

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