简体   繁体   English

查找相对于另一个 WebElement 的 WebElement

[英]Finding WebElement Relative to Another WebElement

I'm trying to find a webelement relative to another webelement that's not a child to the other using Selenium.我试图找到一个相对于另一个 webelement 的 webelement,该 webelement 不是另一个使用 Selenium 的子元素。 Here is a snippet of the source I'm working with ...这是我正在使用的源代码片段...

<td class="action" rowspan="7">
    <table class="action">
        <tbody>
            <tr class="topTr topTrPlainBg">
            <tr>
                <td class="middleLeftPlainBg" style="width: 4px;">
                <td class="caption" style="width: 99%; height: 215px;" colspan="4" title="Action properties">
                    <a href="javascript: var none=0;">Foo bar</a>
                </td>
                <td class="middleRightPlainBg" style="width: 4px;">
            </tr>
            <tr class="bottomTr bottomTrPlainBg">
        </tbody>
    </table>
</td>
<td class="rule rulePlainBg" colspan="1">
    <table class="rule rulePlainBg">
        <tbody>
            <tr>
                <td class="captionRule">Successful</td>
                <td class="plus">
                    <img src="https://mcc-69-77.usae.bah.com/sam/admin/vpe2/public/img/vpe-old/rule/plus-over.gif" title="Add item"/>
                </td>
                <td class="swap">
            </tr>
        </tbody>
    </table>
</td>

I'm trying to find the img element with the title "Add item" relative to the a element with the value of "Foo bar".我正在尝试相对于值为“Foo bar”的a元素查找标题为“添加项目”的img元素。 I know the element I'm looking for is going to be a child of the td immediately following the td that's the parent of the relative element.我知道我正在寻找的元素将是紧跟在相关元素父元素td之后的td的子元素。 Here's what I have so far (in Java) ...这是我到目前为止所拥有的(在 Java 中)......

WebElement fooBar = driver.findElement(By.linkText("Foo bar"));
fooBar.findElement(By.xpath("..//..//..//..//../td//img[@title='Add item']")).click();

Any ideas what I'm doing wrong here?任何想法我在这里做错了什么? I've tried playing around with the slashes a bit but can't get anything to work.我试过稍微玩弄斜线,但什么也做不了。

What if you change the strategy a bit and use following-sibling :如果您稍微改变策略并使用following-sibling怎么办:

//td[@class="action" and .//td[@class="caption"]/a[. = "Foo Bar"]]/following-sibling::td[contains(@class, "rule")]//td[@class="plus"]/img[@title="Add item"]

In selenium:在硒中:

WebElement addItem = driver.findElement(By.xpath("//td[@class='action' and .//td[@class='caption']/a[. = 'Foo Bar']]/following-sibling::td[contains(@class, 'rule')]//td[@class='plus']/img[@title='Add item']"));
addItem.click();

Here we are getting the a element with Foo Bar text checking parent's classes on the way.在这里,我们正在获取带有Foo Bar文本的a元素,在途中检查父类的类。 Then, for the first td parent getting the following td sibling with rule class.然后,对于第一个td父级,获得以下具有rule类的td兄弟级。 Then, getting the desired img element.然后,获取所需的img元素。

And if you want to start from the found a element, use ancestor to find the td parent with class="action" and then apply the following sibling check:如果你想从找到的a元素开始,使用ancestor找到class="action"td父元素,然后应用以下兄弟检查:

ancestor::td[@class="action"]/following-sibling::td[contains(@class, "rule")]//td[@class="plus"]/img[@title="Add item"]

In selenium:在硒中:

WebElement fooBar = driver.findElement(By.linkText("Foo bar"));
fooBar.findElement(By.xpath("ancestor::td[@class='action']/following-sibling::td[contains(@class, 'rule')]//td[@class='plus']/img[@title='Add item']")).click();

查看您正在使用的xpath ,我看到的是您缺少初始值//因此请替换您正在使用的xpath

//..//..//..//..//../td//img[@title='Add item']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM