简体   繁体   English

获取属性指定为“的所有标签中包含的文本<div class='wld' text > &quot; 使用 Python 中的 WebDriver Selenium

[英]Get the text contained in all the tag having the attribute specify as "<div class='wld' text >" with the WebDriver Selenium in python

I would like to recieve the "text" contained in all the <div class='wld' text > .我想收到包含在所有<div class='wld' text > I tried this code :我试过这个代码:

match_results = driver.find_elements(By.CSS_SELECTOR, ".wld").text
 
print(match_results)

and this和这个

  match_results = driver.find_elements(By.CSS_SELECTOR, ".wld")
     
    print(match_results.text)

and as well try the methods answered this question How to get text with selenium web driver in python that resemble the method that I did more high.并尝试回答这个问题的方法如何在 python使用 selenium web 驱动程序获取文本,类似于我做的更高的方法。

I get this error message :我收到此错误消息:

 match_results = driver.find_elements(By.CSS_SELECTOR, ".wld").text
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'text'

and this和这个

print(match_results.text)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'text'

When I Remove .text here is what this当我删除.text这里是什么

[<selenium.webdriver.remote.webelement.WebElement (session="409c9b33d38ef268b14fe6f18992b61c", element="2321a252-8cb9-4fd1-8f8a-af837c28b35c")>,

and this for each element "text" contained in <div class='wld' text > What can I do for display all the "text", method to used ?这对于<div class='wld' text >包含的每个元素“文本”我可以做什么来显示所有“文本”,使用的方法? I'm heard that it would have to through BeautifulSoup in others forum, your notice ?我听说它必须通过其他论坛的 BeautifulSoup,您注意到了吗?

Important information that I forgeted of precis : it is that I arrive to access the first element of way individual with method driver.find_element(By.CSS_SELECTOR, ".wld").text for exemple and.我忘记了 precis 的重要信息:例如,我使用方法driver.find_element(By.CSS_SELECTOR, ".wld").text访问方式个体的第一个元素。

'find_elements' method will return a list of elements and it does not have 'text' attribute. 'find_elements' 方法将返回一个元素列表,它没有 'text' 属性。

So if you want to get list of elements try this.因此,如果您想获取元素列表,请尝试此操作。

match_results = driver.find_elements(By.CSS_SELECTOR, ".wld")
for elem in match_results:
   print(elem.text)

Or if you want to get only one element try this.或者,如果您只想获取一个元素,请尝试此操作。

match_results = driver.find_element(By.CSS_SELECTOR, ".wld").text

Selenium python:获取所有<li>所有的文本<ul>从一个<div></div><div id="text_translate"><p>我想从几页中获取所有作为dutch word = english word的单词列表。</p><p> 通过检查 HTML,这意味着我需要从#mw-content-text的子 div 中获取所有ul的所有li中的所有文本。</p><p> 这是我的代码:</p><pre> from selenium import webdriver options = webdriver.ChromeOptions() options.add_argument('headless') # start chrome without opening window driver = webdriver.Chrome(chrome_options=options) listURL = [ "https://duolingo.fandom.com/wiki/Dutch_(NL)_Skill:Basics_1", "https://duolingo.fandom.com/wiki/Dutch_(NL)_Skill:Basics_2", "https://duolingo.fandom.com/wiki/Dutch_(NL)_Skill:Phrases_1", "https://duolingo.fandom.com/wiki/Dutch_(NL)_Skill:Negative_1", ] list_text = [] for url in listURL: driver.get(url) elem = driver.find_elements_by_xpath('//*[@id="mw-content-text"]/div/ul') for each_ul in elem: all_li = each_ul.find_elements_by_tag_name("li") for li in all_li: list_text.append(li.text) print(list_text)</pre><p> 这是 output</p><pre> ['man = man', 'vrouw = woman', 'jongen = boy', 'ik = I', 'ben = am', 'een = a/an', 'en = and', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']</pre><p> 我不明白为什么有些li文本即使它们的 xpath 相同也无法检索(我通过开发人员控制台的副本 xpath 仔细检查了其中的几个)</p></div></ul></li> - Selenium python: get all the <li> text of all the <ul> from a <div>

暂无
暂无

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

相关问题 使用Python和Selenium Webdriver从textarea获取文本(无值属性) - Get text from textarea using Python & Selenium Webdriver (no value attribute) 如何在 python selenium webdriver 中获取 div 属性文本 - how to get div properties text in python selenium webdriver 在 Python 中使用 Selenium WebDriver 获取文本时出现问题 - Having problem getting text with Selenium WebDriver in Python Selenium python:获取所有<li>所有的文本<ul>从一个<div></div><div id="text_translate"><p>我想从几页中获取所有作为dutch word = english word的单词列表。</p><p> 通过检查 HTML,这意味着我需要从#mw-content-text的子 div 中获取所有ul的所有li中的所有文本。</p><p> 这是我的代码:</p><pre> from selenium import webdriver options = webdriver.ChromeOptions() options.add_argument('headless') # start chrome without opening window driver = webdriver.Chrome(chrome_options=options) listURL = [ "https://duolingo.fandom.com/wiki/Dutch_(NL)_Skill:Basics_1", "https://duolingo.fandom.com/wiki/Dutch_(NL)_Skill:Basics_2", "https://duolingo.fandom.com/wiki/Dutch_(NL)_Skill:Phrases_1", "https://duolingo.fandom.com/wiki/Dutch_(NL)_Skill:Negative_1", ] list_text = [] for url in listURL: driver.get(url) elem = driver.find_elements_by_xpath('//*[@id="mw-content-text"]/div/ul') for each_ul in elem: all_li = each_ul.find_elements_by_tag_name("li") for li in all_li: list_text.append(li.text) print(list_text)</pre><p> 这是 output</p><pre> ['man = man', 'vrouw = woman', 'jongen = boy', 'ik = I', 'ben = am', 'een = a/an', 'en = and', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']</pre><p> 我不明白为什么有些li文本即使它们的 xpath 相同也无法检索(我通过开发人员控制台的副本 xpath 仔细检查了其中的几个)</p></div></ul></li> - Selenium python: get all the <li> text of all the <ul> from a <div> 如何获取 python 中 div 标签的 title 属性的文本值? - how to get text value of title attribute of div tag in python? 在Angular网站中获取确切的文本 <div> 用Selenium和Python标记? - In Angular website, get exact text inside <div> tag with Selenium & Python? 如何在 For 循环 Python 中使用 Selenium WebDriver 获取文本 - How to get text with Selenium WebDriver in a For Loop Python 如何在 Python 中使用 Selenium WebDriver 获取文本 - How to get text with Selenium WebDriver in Python 试图在python中使用硒从div类中获取文本 - Trying to get Get text out of div class using selenium in python 硒:通过python选择div标签和文本 - selenium : choose a div tag and text via python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM