如何在 Python 中使用 Selenium 查找元素内的元素? [英] How do I find elements inside of elements using Selenium with Python?

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

问题描述

我无法弄清楚返回此 HTML 示例中特定元素的语法.

我尝试过的是 driver.find_elements_by_tag_name('div.calibre.div') 并且还使用('div.calibre .div') |('div.calibre + div') 和其他变体.我已经设法返回页面中的所有 div 元素,但我只想返回嵌套在 calibre 类中的所有元素(即书籍、标题页、书籍和书籍).有没有办法做到这一点?

francisco sollima 给了我正确的答案,我现在要分享它.在我原来的问题中,我没有注意到 <div > 与类 <calibre > 在 HTML 文档的底部关闭,因此使用的语法返回的对象比预期的多.这是修订后的 HTML,以防有人需要此信息.

要查找顶部 < 下的所有元素div > 即 <div class='titlepage' > 语法是 driver.find_elements_by_class_name('titlepage').find_elements_by_tag_name('div'),或者语法可以是 driver.find_element_by_tag_name('div.titlepage').find_elements.by_tag_name('div').

这将返回 < 下的嵌套元素.div class='titlepage'>.

解决方案

如果只有一个属于 'calibre' 类的元素,而它正是您需要的,您可以这样做:

driver.find_element_by_class_name('calibre').find_elements_by_tag_name('div')

当您执行 driver.find_element_by_class_name('calibre')(或任何其他 driver.find... 函数)时,该调用返回一个对象具有与驱动程序相同的方法.换句话说,您可以像浏览整个网页一样浏览网页的一部分.

在这种情况下,driver.find_element_by_class_name('calibre') 将返回网页的一部分,其中包含类为 'calibre' 的 de 元素及其所有子元素.要访问它的 div,您应该使用 find_elements_by_tag_name 导航它.

I can't figure out the syntax to return the specific elements in this HTML example.

<div class="calibre" id="calibre_link-0">
    <div class="book" title="Chapter 11. Web Scraping">
        <div class="titlepage">
            <div class="book">
                <div class="book">
                    <h1 class="title1"><a id="calibre_link-2915" class="firstname"></a>Web Scraping</h1>
                </div>
            </div>
        </div>

What I have tried is driver.find_elements_by_tag_name('div.calibre.div') and also using ('div.calibre .div') | ('div.calibre + div') and other variations on those. I have managed to return all the div elements in the page but I just want to return all of the ones nested within the calibre class (i.e. book, titlepage, book, and book). Is there a way to do this?

EDIT: francisco sollima gave me the proper answer and I'm going to share it now. In my original question I didn't notice that the < div > with the class < calibre > closed at the bottom of the HTML document so the syntax used was returning more objects than expected. This is revised HTML in case anyone needs this information.

<div class='titlepage'>
    <div class='book'>
        <div class='book'>
        </div>
    </div>
</div>

To find all of the elements under the top < div > i.e. < div class='titlepage' > the syntax would be driver.find_elements_by_class_name('titlepage').find_elements_by_tag_name('div'), alternately the syntax could be driver.find_element_by_tag_name('div.titlepage').find_elements.by_tag_name('div').

This will return the nested elements under the < div class='titlepage' >.

解决方案

If there's only one element with the class 'calibre' and it's the one you need, you can do:

driver.find_element_by_class_name('calibre').find_elements_by_tag_name('div')

When you execute driver.find_element_by_class_name('calibre') (or any other driver.find... function for that matter), that call returns an object that has the same methods as driver. In other words, you get a piece of the webpage that you can navigate as you would the whole webpage.

In this case, driver.find_element_by_class_name('calibre') will return a portion of the webpage with de element whose class is 'calibre' and all it's children. To access it's divs, you should navigate it with find_elements_by_tag_name.

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

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