简体   繁体   English

如何使用 selenium 获取 python 中元素的所有子元素?

[英]How to get all children of an element in python using selenium?

How can I turn this JavaScript into Python to get all child elements from the parent?如何将此 JavaScript 转换为 Python 以从父级获取所有子元素?

This script gets all the elements from the google.com site via console此脚本通过控制台从 google.com 站点获取所有元素

e = document.getElementsByTagName('body')[0].children

for (let i = 0; i < e.length;i++){
    console.log(e[i].tagName)
}

In python I tried to do this, but I can't在 python 我试图这样做,但我不能

import time
import requests
import pandas as pd
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.firefox import options
from selenium.webdriver.firefox.options import Options

option = Options()
option.headless = True
driver = webdriver.Firefox(executable_path=r'C:\geck\geckodriver.exe')

driver.get('https://www.google.com.br/')
body = driver.find_element_by_tag_name('body')
childrens = body.childrens

for i in childrens:
    print(i.tagName)

driver.quit()

Commands used to install packages:用于安装软件包的命令:

pip install pandas
pip install bs4
pip install selenium
pip install requests

How can I get the element names from a body tag in python?如何从 python 中的主体标签中获取元素名称?

To turn this JavaScript into Python to get all child elements from the parent将此JavaScript转换为Python以从父级获取所有子元素

e = document.getElementsByTagName('body')[0].children

for (let i = 0; i < e.length;i++){
    console.log(e[i].tagName)
}

in a simple and crispy way you just need to pass the JavaScript within execute_script() as follows:以简单明了的方式,您只需在execute_script()中传递JavaScript ,如下所示:

driver.get("https://www.google.com.br/")
driver.execute_script("""
    e = document.getElementsByTagName('body')[0].children
    for (let i = 0; i < e.length;i++){
    console.log(e[i].tagName)
}
""")

Snapshot of the Console:控制台快照:

JavaScript

You can use body.find_elements_by_xpath("./child::*") to get all the child elements in body as WebElement object and then get their tag name by accessing the tag_name attribute您可以使用body.find_elements_by_xpath("./child::*")将 body 中的所有子元素作为WebElement object 获取,然后通过访问tag_name属性获取它们的标签名称

import time
import requests
import pandas as pd
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.firefox import options
from selenium.webdriver.firefox.options import Options

option = Options()
option.headless = True
driver = webdriver.Firefox(executable_path=r'C:\geck\geckodriver.exe')

driver.get('https://www.google.com.br/')
body = driver.find_element_by_tag_name('body')
childrens = body.find_elements_by_xpath("./child::*")

for i in childrens:
    print(i.tag_name)

driver.quit()

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

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