Is it possible to find common xpath for this elements?

75 Views Asked by At

xpath of one element is: //div[@class=name-slider-header']//button//img[2]

xpath of another element is: //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"); element2.getAttribute("src").contains("red");

Is it possible to find common xpath for this elements?

2

There are 2 best solutions below

0
KunduK On BEST ANSWER

Use the following xpath to identify the image elements where src value contains red

//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')]"));
4
Prophet On

The common XPath of these 2 elements is //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
  }
}