在硒中查找WebElements的子节点 [英] Locating child nodes of WebElements in selenium

查看:153
本文介绍了在硒中查找WebElements的子节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用硒来测试我的Web应用程序,并且可以使用By.xpath成功地找到标签.但是,有时我需要在该节点中找到子节点.

I am using selenium to test my web application and I can successfully find tags using By.xpath. However now and then I need to find child nodes within that node.

示例:

<div id="a">
    <div>
        <span />
        <input />
    </div>
</div>

我可以做到:

WebElement divA = driver.findElement( By.xpath( "//div[@id='a']" ) )

但是现在我需要找到输入,所以我可以这样做:

But now I need to find the input, so I could do:

driver.findElement( By.xpath( "//div[@id='a']//input" ) )

但是,到那时代码中我只有divA了,不再有它的xpath了……我想做这样的事情:

However, at that point in code I only have divA, not its xpath anymore... I would like to do something like this:

WebElement input = driver.findElement( divA, By.xpath( "//input" ) );

但是这种功能不存在. 我可以这样做吗?

But such a function does not exist. Can I do this anyhow?

BTW:有时我需要找到一个具有一定下降节点的<div>.我如何在xpath中询问包含文本为'hello world'<span><div>"?

BTW: Sometimes I need to find a <div> that has a certain decendent node. How can I ask in xpath for "the <div> that contains a <span> with the text 'hello world'"?

推荐答案

根据

According to JavaDocs, you can do this:

WebElement input = divA.findElement(By.xpath(".//input"));


如何在xpath中询问包含跨度的div标签, 文字"hello world""?

How can I ask in xpath for "the div-tag that contains a span with the text 'hello world'"?

WebElement elem = driver.findElement(By.xpath("//div[span[text()='hello world']]"));

XPath规范是对此的惊人阅读.

这篇关于在硒中查找WebElements的子节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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