首页 > 解决方案 > 如何使用 selenium 访问嵌套的 html 元素?

问题描述

我正在使用一个学校课程表网站,我想访问 div 元素,其中包含有关班级中有多少个座位以及谁在教它的信息以便抓取它。我首先找到包含我想要的 div 元素的元素,然后我尝试使用 xpaths 找到我想要的 div 元素。我面临的问题是,当我尝试使用 find_element_by_xpath 或 find_elements_by_xpath 来获取我想要的 div 时,我得到了这个错误:

'list' object has no attribute 'find_element_by_xpath'

发生此错误是因为我要查找的 div 元素是嵌套的吗?有没有办法使用 div 标签获取嵌套元素?

这是我目前拥有的代码:


driver = webdriver.Chrome(ChromeDriverManager().install())
url = "https://app.testudo.umd.edu/soc/202008/INST"
driver.get(url)
section_container = driver.find_elements_by_id('INST366')

sixteen_grid = section_container.find_element_by_xpath(".//div[@class = 'sections sixteen colgrid']").text 



我想要的信息是这样的:


<div class = "sections sixteen colgrid"</div>

它目前在这个 id 标签内:

<div id="INST366" class="course"</div>

如果有人可以帮助我解决这个问题,我们将不胜感激

标签: pythonhtmlselenium

解决方案


From documentation of find_elements_by_id:

Returns : list of WebElement - a list with elements if any was found. An empty list if not

Which means section_container is a list. You can't call find_element_by_xpath on a list but you can on each element within the list because they are WebElement.

What says the documentation about find_element_by_id?

Returns : WebElement - the element if it was found

In this case you can use find_element_by_xpath directly. Which one you should use? Depends on your need, if need to find the first match to keep digging for information or you need to use all the matches.

After fixing that you will encounter a second problem: your information is displayed after executing javascript code when clicking on "Show Sections", so you need to do that before locating what you want. For that go get the a href and click on it.

The new code will look like this:

from selenium import webdriver
from time import sleep

driver = webdriver.Chrome()
url = "https://app.testudo.umd.edu/soc/202008/INST"
driver.get(url)
section_container = driver.find_element_by_id('INST366')
section_container.find_element_by_xpath(".//a[@class='toggle-sections-link']").click()
sleep(1)
section_info = section_container.find_element_by_xpath(".//div[@class='sections sixteen colgrid']").text
driver.quit()

推荐阅读