ImageTag是否存在于TR中? [英] ImageTag exists in the TR?

查看:65
本文介绍了ImageTag是否存在于TR中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

    string name = String.Format("//*[contains(text(), \'{0}\')]", 'TEST1');
    IWebElement element = table.FindElement(By.XPath(name));
    IWebElement parent = element.FindElement(By.XPath(".."));

    try
    {
        IWebElement image = element.FindElement(By.XPath("//img"));
    }
    catch (NoSuchElementException e)
    {
        return false;
    }

    return true;
}

我如何确定TEST1是否具有Image?在下面的html源代码中,我有tr,而在tr内我有td,有些tr可能带有图片标签,而有些则没有

How would I find out if the TEST1 does have Image? in the below html source code, I have tr and within tr i have td and some tr may have image tag and some may not

因此,我将传递一个示例名称:TEST1,并且在返回中,我希望该名称是否具有Image标签.

So, I will be passing name for an example: TEST1 and in returns I will be expecting if the name has Image tag or not.

,如果我通过TEST2TEST3,则应该return null,因为它没有图像标签,并且TEST1TEST4确实具有Image tag,因此应该返回我是真的.

again, if I pass TEST2 and TEST3 it should return null since it does not have an image tag and where as TEST1 and TEST4 does have Image tag hence it should return me true.

我尝试了类似的方法,但是没有用:

I tried something like this but did not work:

string name = String.Format(".//td[contains(., \'{0}\')]/..//@src", "TEST1");
IWebElement element = driver.FindElement(By.XPath(name));

遇到此错误:尝试上述代码后...

get this error: after trying the above code...

xpath表达式'.//td [contains(.,'TEST1')]/..//@ src' 无法评估或无法生成WebElement

The xpath expression './/td[contains(., 'TEST1')]/..//@src' cannot be evaluated or does notresult in a WebElement

下面是html源代码

<div id="DIV_ID_1">
    <table id="TBLID1">
        <tr>
            <td>
                TEST1
            </td>
            <td>
                <img id="ctl00" src="../App_Themes/Default/images/phone.gif" />
            </td>
        </tr>
        <tr>
            <td>
                TEST2
            </td>
        </tr>
        <tr>
            <td>
                TEST3
            </td>
        </tr>
        <tr>
            <td>
                TEST4
            </td>
            <td>
                <img id="ctl02" src="../App_Themes/Default/images/phone.gif" />
            </td>
        </tr>
    </table>
</div>

推荐答案

所以我将其分解为几个步骤.

So I would break this up into a few steps.

首先获取您的元素:

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

然后获取父元素:

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

然后检查父元素中的<img>标签:

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.

类似的东西:

public WebElement getImage(String innerText)

然后只需传递TEST1或TEST2

then just pass in TEST1 or TEST2

这篇关于ImageTag是否存在于TR中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆