简体   繁体   English

使用python selenium webdriver查找html页面中的所有子元素

[英]Finding all child elements in an html page using python selenium webdriver

I want to extract all h2 elements of the div element.我想提取 div 元素的所有 h2 元素。 The code that I've used is this:我使用的代码是这样的:

browser = webdriver.Chrome()
browser.get("https://www.mmorpg.com/play-now")
time.sleep(2)
item_list_new=[]
link = browser.find_element_by_xpath("//div[@class='freegamelist']")
names = link.find_element_by_tag_name('h2')
x = names.text
item_list_new.append(x)
print(item_list_new)

But when I run this, I only get the first 'h2' element of the div element.但是当我运行它时,我只得到 div 元素的第一个 'h2' 元素。 Can somebody tell me what am I doing wrong and also please guide me with the correct way of doing it?有人可以告诉我我做错了什么,还请指导我正确的做法吗? Thanks in advance.提前致谢。

you need to write names = link.find_elements_by_tag_name('h2')你需要写names = link.find_elements_by_tag_name('h2')

Your code should be你的代码应该是

browser = webdriver.Chrome()
browser.get("https://www.mmorpg.com/play-now")
time.sleep(2)
item_list_new=[]
link = browser.find_element_by_xpath("//div[@class='freegamelist']")
names = link.find_elements_by_tag_name('h2')
x = names.text
item_list_new.append(x)
print(item_list_new)

find_element_by_tag_name gives the first element and find_elements_by_tag_name gives all the matching elements find_element_by_tag_name给出第一个元素, find_elements_by_tag_name给出所有匹配的元素

您实际上想要使用听起来几乎相似的函数find_elements_by_tag_name ,正如这里所指出的。

Try to get all header values as below:尝试获取所有标头值,如下所示:

link = browser.find_element_by_xpath("//div[@class='freegamelist']")
names = link.find_elements_by_tag_name('h2')
item_list_new = [x.text for x in names]
print(item_list_new)

or you can simplify或者你可以简化

names = browser.find_elements_by_xpath("//div[@class='freegamelist']//h2")
item_list_new = [x.text for x in names]
print(item_list_new)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM