selenium怎么使用使⽤selenium解析本地HTML⽂件⽅法import requests
import time
"""
经验总结:
1.使⽤selenium解析本地HTML⽂件⽅法,
<("file://C:/Users/23242/Desktop/HTML/People List _ USDA ARS.html")
file://很重要不能少;
2.(.text)获取不到时,换成.get_attribute("textContent")
3.(.replace('\n','')).replace(' ',''))替换空格和换⾏
4.有时候界⾯的弹出框,并不是⼀个⼦ifram 其实代码就在界⾯上,注意观察
"""
from selenium import webdriver
browser=webdriver.Chrome()
<("file://C:/Users/Desktop/HTML/People List _ USDA ARS.html")
# print(browser.page_source)
# time.sleep(3)
#
button=browser.find_element_by_xpath('//*[@id="prefix-overlay-header"]/button')
button.click()
# 打开所有界⾯
# button=browser.find_element_by_id('expandAll')
# button.click()
title=browser.find_elements_by_xpath('//ul[@class="usa-accordion-bordered"]/li/button')
print(len(title))
for i in range(0,len(title)):
# print(title[i].text)#打印标题  列表是从0开始  xpath是从1开始
path=('//ul[@class="usa-accordion-bordered"]/li[{0}]/div/ul/li/a').format(i+1)
# print(path)
name=browser.find_elements_by_xpath(path)
# print(len(name))
for j in range(0,len(name)):
name_little=((name[j].get_attribute("textContent")).replace('\n','')).replace(' ','')
# print(name_little)
# print('\n')#打印名字
path_one=('//ul[@class="usa-accordion-bordered"]/li[{0}]/div/ul/li[{1}]').format(i+1,j+1)
# print(path_one)
name_content=browser.find_elements_by_xpath(path_one)
for k in range(len(name_content)):  #打印名字的内容
cont=((name_content[k].get_attribute("textContent")).replace('\n','')).replace(' ','')
print(cont)

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。