ImageTag exists in the TR?

前端 未结 2 1125
野性不改
野性不改 2021-01-22 19:20

EDIT:

public bool getImage()
{
    IWebElement table = driver.FindElement(By.Id(\"DIV_ID_1\"));

    string name = String.         


        
相关标签:
2条回答
  • 2021-01-22 19:53

    So I would break this up into a few steps.

    First get your element:

    WebElement element = driver.findElement(By.xpath("//*[contains(text(), 'TEST1')]"));
    

    Then get the parent element:

    WebElement parent = element.findElement(By.xpath(".."));
    

    Then check the parent element for an <img> tag:

    Try
    {
        WebElement image = parent.findElement(By.xpath("//img"));
    }
    catch (NoSuchElementException e)
    {
        System.out.println("Did not find an image");
    }
    

    I'd wrap this in a function that I could then pass in the text to find the image and return the element if it exists.

    Something like:

    public WebElement getImage(String innerText)
    

    then just pass in TEST1 or TEST2

    0 讨论(0)
  • 2021-01-22 19:55

    Using your original XPath, it works fine, executed both in Firefox and Chrome's developer tools and it returns the img. Does it actually return anything or just doesn't return what you'd expect? Does it error saying the element cannot be found?

    This is another way of getting it:

    //td[normalize-space(text())='TEST1']/../descendant::img/@src
    
    0 讨论(0)
提交回复
热议问题