Home > Software engineering >  Is it possible to find common xpath for this elements?
Is it possible to find common xpath for this elements?

Time:01-25

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?

CodePudding user response:

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
  }
}

CodePudding user response:

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')]"));
  • Related