简体   繁体   English

Python Selenium 从 class 中获取文本

[英]Python Selenium get text out of a class

Hey I want to have the text which is in this class and print it in the console.嘿,我想要这个 class 中的文本并将其打印在控制台中。
How can I do this?我怎样才能做到这一点?
I have tried:我努力了:

profileName = driver.find_element_by_class_name("._7UhW9.fKFbl.yUEEX.KV-D4.fDxYl").text()
print(profileName)

I would be very happy if you could help me.如果你能帮助我,我会很高兴。
Tom Rudolph汤姆·鲁道夫

In case that element has all these class names you just have to change from find_element_by_class_name to find_element_by_css_selector , as following:如果该元素具有所有这些 class 名称,您只需将find_element_by_css_selector find_element_by_class_name如下所示:

profileName = driver.find_element_by_css_selector("._7UhW9.fKFbl.yUEEX.KV-D4.fDxYl").text()
print(profileName)

However I'm quite sure these class names are dynamically changing so this will not work.但是我很确定这些 class 名称是动态变化的,所以这是行不通的。
Also, make sure you added some delay / wait before accessing the element.另外,请确保在访问元素之前添加了一些延迟/等待。

maybe try this is from the BeautifulSoup library也许试试这个来自 BeautifulSoup 图书馆

from the beginning:从一开始就:

import requests
from bs4 import BeautifulSoup

url = 'my_site'
r = request.get (url)
re1 = BeautifulSoup (r.text, "lxml")
re2 = re1.body.find_all ('div', {'class': '._7UhW9.fKFbl.yUEEX.KV-D4.fDxYl'})
for syntax in re2 [:]:
     print (syntax)

where there is div you can put another tag where this class is (a, p, h) where there is div you can put another tag where this class is (a, p, h)在有 div 的地方,你可以在 class 是 (a, p, h) 的地方放另一个标签 在有 div 的地方,你可以在 class 是 (a, p, h)

and in selenium, instead of 'element' use 'elements', when harvesting it is better to collect everything: D But when you want to click an element or type something (send_keys () or click ()) you have to use a single number of characters, which is 'element' and not 'elements'在 selenium 中,不要使用“元素”,而是使用“元素”,当收获时最好收集所有东西:D 但是当你想点击一个元素或输入一些东西(send_keys()或点击())你必须使用一个字符数,这是“元素”而不是“元素”

I do this for seleneium webdriver我为 selenium webdriver 做这个

data1 = driver.find_elements_by_class_name('class')
data2 = driver.find_elements_by_css_selector('css_selector")

if len(data1) >  0:
  do something
if len(data2) > 0:
 do something

you can always add an else if you want to see how the error pops up, print (len (data1)) or data2 to see how much data you have collected etc.如果你想查看错误是如何弹出的,你总是可以添加一个 else,print (len (data1)) 或 data2 来查看你收集了多少数据等。

it not text() in python, it is .text它不是 python 中的text() ,它是.text

also this ._7UhW9.fKFbl.yUEEX.KV-D4.fDxYl looks dynamic and a css_selector not class_name .这个._7UhW9.fKFbl.yUEEX.KV-D4.fDxYl看起来也是动态的,而且css_selector不是class_name

Code 1:代码 1:

profileName = driver.find_element_by_css_selector("._7UhW9.fKFbl.yUEEX.KV-D4.fDxYl").text
print(profileName)

Code 2:代码 2:

profileName = driver.find_element_by_css_selector("._7UhW9.fKFbl.yUEEX.KV-D4.fDxYl").get_attribute('innerHTML')
print(profileName)

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

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