因此,我将其分解为几个步骤。
首先获取您的元素:
WebElement element = driver.findElement(By.xpath("//*[contains(text(), 'TEST1')]"));然后获取父元素:
WebElement parent = element.findElement(By.xpath(".."));然后检查父元素是否有
<img>标签:
Try{ WebElement image = parent.findElement(By.xpath("//img"));}catch (NoSuchElementException e){ System.out.println("Did not find an image");}我将其包装在一个函数中,然后可以传递文本以查找图像并返回元素(如果存在)。
就像是:
public WebElement getImage(String innerText)
然后只需传递TEST1或TEST2



