Selenium Webdriver 在子元素中查找元素

Posted

技术标签:

【中文标题】Selenium Webdriver 在子元素中查找元素【英文标题】:Selenium Webdriver finding an element in a sub-element 【发布时间】:2012-12-12 13:35:21 【问题描述】:

我正在尝试使用 Selenium(版本 2.28.0)在子元素中搜索元素,但 selenium des 似乎并未将其搜索限制在子元素中。我做错了还是有办法使用 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')

它从第二个分区返回 html。所以 selenium 并没有将其搜索限制在 element2 上。

我希望能够找到 element2 的子元素。这篇文章表明我的代码应该可以工作 Selenium WebDriver access a sub element 但他的问题是由超时问题引起的。

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

【问题讨论】:

【参考方案1】:

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

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

【讨论】:

这是 WebDriver 中一个不幸的设计错误,它已作为一项功能被奉为神圣。 WebDriver 中的所有其他element.find_element_by_whatever() 方法执行在指定元素的上下文中 的搜索。 find_element_by_xpath() 没有。 Selenium bug #403 更详细。 这不是 WebDriver 的设计错误,这是 XPath 规范要求的。 这绝对不是设计错误——这就是所有 DOM 选择在 javascript 中的工作方式...... 我推荐这个站点来学习不同的 XPath 命令 - scientecheasy.com/2019/08/xpath-axes.html【参考方案2】:

使用以下内容:

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

如果您有任何问题,请告诉我。

【讨论】:

.text 不返回锚标记的文本值。这个是element.get_attribute("text") find_element_by_css_selector【参考方案3】:

这是您在 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')

【讨论】:

【参考方案4】:

Chrome 网络驱动程序:

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

【讨论】:

以上是关于Selenium Webdriver 在子元素中查找元素的主要内容,如果未能解决你的问题,请参考以下文章

转载selenium+Python WebDriver之元素定位

Python3.x:Selenium中的webdriver进行页面元素定位

selenium学习笔记webdriver进行页面元素定位

Selenium Webdriver:元素不可见异常

Selenium C# WebDriver:等到元素出现

python selenium-webdriver 元素操作之键盘操作