Python Selenium Webdriver 在子元素中查找元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14049983/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 10:17:33  来源:igfitidea点击:

Selenium Webdriver finding an element in a sub-element

pythonxpathselenium

提问by Dominic Larkin

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?

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

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:

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

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.

它从第二个部门返回 html。所以 selenium 并没有将它的搜索限制在 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 elementbut his problem was caused by a time-out issue.

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

Can anyone help me understand what is happening here?

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

采纳答案by p0deje

When you start your XPath expression with //, it search from root of document ignoring your parent element. You should prepend expression with .

当您使用 开始 XPath 表达式时//,它会从文档的根目录开始搜索,而忽略您的父元素。你应该在表达式前面加上.

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

回答by user3487861

Use the following:

使用以下内容:

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

Please let me know if you have any problems.

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

回答by Harvey

This is how you search for element or tag in CSS subclass and I believe that it works for multilevel situation as well:

这就是你在 CSS 子类中搜索元素或标签的方式,我相信它也适用于多级情况:

Sample HTML:

示例 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>

This is how you would get pubdatetag value for example.

例如,这就是您获取pubdate标签值的方式。

published = driver.find_element_by_css_selector('li>time').get_attribute('datetime')