Selenium Webdriver在子元素中查找元素

94

我正在尝试使用Selenium(版本2.28.0)在子元素中查找元素,但是Selenium似乎没有将搜索限制在子元素中。我是做错了还是有一种方法可以使用element.find在子元素中进行搜索?

例如,我创建了一个简单的测试网页,其中包含以下代码:

<!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>

我的Python代码(版本2.6)看起来像这样:

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."

如果我运行:

print element2.get_attribute('innerHTML')
它返回第二个division的html。 因此,selenium没有限制其搜索范围只在元素2中。
我希望能够找到element2的子元素。这篇文章建议我的代码应该可以工作Selenium WebDriver访问子元素但他的问题是由于超时问题引起的。
有人可以帮我理解这里发生了什么吗?
6个回答

186
如果您以//开头的XPath表达式,它将从文档的根开始搜索。为了相对于特定元素进行搜索,应该使用.来在表达式前加上引导符号:
element2 = driver.find_element_by_xpath("//div[@title='div2']")
element2.find_element_by_xpath(".//p[@class='test']").text

5
WebDriver 中存在一种不幸的设计错误,已经被奉为特性。WebDriver 中的所有其他 element.find_element_by_whatever() 方法都是在指定元素的上下文中执行搜索。但是,find_element_by_xpath() 不是这样的。更多详细信息请参见 Selenium bug #403 - Ross Patterson
10
这不是WebDriver设计错误,而是XPath规范要求的。 - p0deje
这绝对不是设计错误 - 这就是javascript中所有DOM选择器的工作方式... - duhaime
我推荐这个网站来学习不同的XPath命令 - https://www.scientecheasy.com/2019/08/xpath-axes.html - Herker

7
请使用以下内容:
element2 = driver.find_element_by_cssselector("css=div[title='div2']")
element2.find_element_by_cssselector("p[@class='test']").text 

如果你遇到任何问题,请告诉我。


1
.text doesn't return text value for anchor tag. this one does element.get_attribute("text") - Lal
find_element_by_css_selector - Enrique Pérez Herrero
2023年的答案请参考 https://dev59.com/xsTsa4cB1Zd3GeqPFL5A#72854301。 - Gabriel Pellegrino

3

我猜,当使用“driver.find_element”时,我们需要使用webdriver.common.by中的方法“By”。

所以……代码必须是:

from selenium import webdriver

driver = webdriver.Firefox()
from selenium.webdriver.common.by import By

element2 = driver.find_element(By.XPATH, "//div[@title='div2']")
element2.find_element(By.XPATH, ".//p[@class='test']").text

1
这是如何在CSS子类中搜索元素或标签的方法,我相信它也适用于多级情况:
示例HTML:

<li class="meta-item">
 <span class="label">Posted:</span>
 <time class="value" datetime="2019-03-22T09:46:24+01:00" pubdate="pubdate">22.03.2019 u 09:46</time>
</li>

这是如何获取pubdate标签值的示例。
published = driver.find_element_by_css_selector('li>time').get_attribute('datetime')

0

查找任何元素的子元素

parent = browser.find_element(by=By.XPATH,value='value of XPATH of Parents')
    child=parent.find_elements(by=By.TAG_NAME,value='value of child path')

0

Chrome Webdriver:

element = driver.find_element_by_id("ParentElement")
localElement = element.find_element_by_id("ChildElement")
print(localElement.text)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接