本文介绍了如何使用 selenium headless 从 div 类中提取文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从以下 div 标签中提取0%":
I'm trying to pull out the "0%" from the following div tag:
<div class="sem-report-header-td-diff ">0%</div>
我目前的代码是:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument('--headless')
driver = webdriver.Chrome(executable_path='mypath/chrome.exe',
chrome_options=options)
url = 'https://www.semrush.com/info/burton.com'
driver.get(url)
driver.implicitly_wait(2)
change_elements = driver.find_elements_by_xpath(xpath='//div[@class="sem-report-header-td-diff "]')
不知道我做错了什么.这适用于 href 标签,但不适用于此.
not sure what I'm doing wrong. This works with href tags, but its not working for this.
推荐答案
根据您共享的 HTML,提取文本 0% 需要使用方法 get_attribute("innerHTML")
,您可以使用以下任一解决方案:
As per the HTML you have shared to extract the text 0% you need to use the method get_attribute("innerHTML")
and you can use either of the following solutions:
css_selector
:
myText = driver.find_element_by_css_selector("div.sem-report-header-td-diff").get_attribute("innerHTML")
xpath
:
myText = driver.find_element_by_xpath("div[@class='sem-report-header-td-diff']").get_attribute("innerHTML")
这篇关于如何使用 selenium headless 从 div 类中提取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!