Selenium Webdriver 在子元素中查找元素 [英] Selenium Webdriver finding an element in a sub-element

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

问题描述

我正在尝试使用 Selenium(版本 2.28.0)搜索子元素中的元素,但 selenium des 似乎并没有将其搜索限制在子元素上.是我做错了还是有办法使用 element.find 来搜索子元素?

例如,我使用以下代码创建了一个简单的测试网页:

<身体><div类=div标题=div1><h1>我的第一个标题</h1><p class='test'>我的第一段.</p>

<div类=div标题=div2><h1>我的第二个标题</h1><p class='test'>我的第二段.</p>

<div类=div标题=div3><h1>我的第三个标题</h1><p class='test'>我的第三段.</p>

</html>

我的 python(2.6 版)代码如下所示:

from selenium import webdriver驱动程序 = webdriver.Firefox()# 用这个 Firefox 实例打开测试页面# element2 获取第二个分区作为 web 元素element2 = driver.find_element_by_xpath("//div[@title='div2']")# 搜索第二个分区,找到类为'test'的段落并打印内容打印 element2.find_element_by_xpath("//p[@class='test']").text# 预期输出:我的第二段."# 实际输出:我的第一段."

如果我跑:

print element2.get_attribute('innerHTML')

它从第二个部门返回 html.所以 selenium 并没有将它的搜索限制在 element2 上.

我希望能够找到 element2 的子元素.这篇文章建议我的代码应该可以运行 Selenium WebDriver 访问子元素 但他的问题是由超时问题引起的.

谁能帮我了解这里发生了什么?

解决方案

如果您以 // 开始 XPath 表达式,它将从文档的根开始搜索.要相对于特定元素进行搜索,您应该在表达式前面加上 . 代替:

element2 = driver.find_element_by_xpath("//div[@title='div2']")element2.find_element_by_xpath(".//p[@class='test']").text

I am trying to search for an element in a sub-element with Selenium (Version 2.28.0), but selenium des not seem to limit its search to the sub-element. Am I doing this wrong or is there a way to use element.find to search a sub-element?

For an example I created a simple test webpage with this code:

<!DOCTYPE html>
<html>
    <body>
        <div class=div title=div1>
            <h1>My First Heading</h1>
            <p class='test'>My first paragraph.</p>
        </div>
        <div class=div title=div2>
            <h1>My Second Heading</h1>
            <p class='test'>My second paragraph.</p>
        </div>
        <div class=div title=div3>
            <h1>My Third Heading</h1>
            <p class='test'>My third paragraph.</p>
        </div>
    </body>
</html>

My python (Version 2.6) code looks like this:

from selenium import webdriver

driver = webdriver.Firefox()

# Open the test page with this instance of Firefox

# element2 gets the second division as a web element
element2 = driver.find_element_by_xpath("//div[@title='div2']")

# Search second division for a paragraph with a class of 'test' and print the content
print element2.find_element_by_xpath("//p[@class='test']").text 
# expected output: "My second paragraph."
# actual output: "My first paragraph."

If I run:

print element2.get_attribute('innerHTML')

It returns the html from the second division. So selenium is not limiting its search to element2.

I would like to be able to find a sub-element of element2. This post suggests my code should work Selenium WebDriver access a sub element but his problem was caused by a time-out issue.

Can anyone help me understand what is happening here?

解决方案

If you start an XPath expression with //, it begins searching from the root of document. To search relative to a particular element, you should prepend the expression with . instead:

element2 = driver.find_element_by_xpath("//div[@title='div2']")
element2.find_element_by_xpath(".//p[@class='test']").text

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

查看全文
相关文章
Python最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆