如何將此 JavaScript 轉換為 Python 以從父級獲取所有子元素?
此腳本通過控制臺從 google.com 站點獲取所有元素
e = document.getElementsByTagName('body')[0].children
for (let i = 0; i < e.length;i ){
console.log(e[i].tagName)
}
在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()
用于安裝軟體包的命令:
pip install pandas
pip install bs4
pip install selenium
pip install requests
如何從 python 的 body 標簽中獲取元素名稱?
uj5u.com熱心網友回復:
您可以使用body.find_elements_by_xpath("./child::*")將 body 中的所有子元素作為WebElement物件獲取,然后通過訪問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()
uj5u.com熱心網友回復:
將此
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/377646.html
標籤:javascript Python 硒
上一篇:如何在python中使用Selenium發送DataFrame
下一篇:將jinja變數傳遞給后端
