从Selenium WebElement中提取文本

莫妮卡的革命

我试图在此页面上获得所有香水的名称

在此处输入图片说明

但是,当我尝试获取Web元素属性的文本时,我什么也没得到。实际上,我尝试了以下几点:

!pip install selenium
!apt-get update # to update ubuntu to correctly run apt install
!apt install chromium-chromedriver
!cp /usr/lib/chromium-browser/chromedriver /usr/bin
import sys
sys.path.insert(0,'/usr/lib/chromium-browser/chromedriver')
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
wd = webdriver.Chrome('chromedriver',chrome_options=chrome_options)
wd.get("https://www.beaute-test.com/parfums_femme.php")
media_content = wd.find_elements_by_class_name("bt__media__content")

恩,media_content我们有几个要素:

[<selenium.webdriver.remote.webelement.WebElement (session="18f3eed8343ccf0c22fc5c7bae48cff7", element="3123e486-a978-4113-b359-c3e9a89c7e95")>,
 <selenium.webdriver.remote.webelement.WebElement (session="18f3eed8343ccf0c22fc5c7bae48cff7", element="4a7b2d4e-0179-4957-ab32-8f37470bc694")>,
 <selenium.webdriver.remote.webelement.WebElement (session="18f3eed8343ccf0c22fc5c7bae48cff7", element="a7fb478d-aa55-46b8-8953-a7b4dd84220e")>,...
]

但这不返回任何内容:

>>>media_content[1].get_attribute('text')
''
>>>media_content[1].text
''

我还尝试直接在我一直在寻找的名称上使用xpath,但它也不会返回任何内容。

那么,如何获取WebElement的文本?

阿伦迪普乔汉

要显示所有25个名称,请执行以下操作。

media_content=wd.find_elements_by_css_selector("div[class='bt__media__content']>p>a>b")
for media in media_content:
    print (media.text)

我也曾经点击同意弹出窗口。

WebDriverWait(wd, 10).until(EC.element_to_be_clickable((By.ID, "didomi-notice-agree-button"))).click()

产出

Libre
L'Interdit
Madame Glamour Suddenly
Gabrielle
My Way
Eau de Parfum - Alien
Idôle
Good Girl
Coco Noir
Scandal
Mon Guerlain
Mon Rouge!
Black Opium Eau de Parfum
Mauboussin à La Folie
Black Orchid
Voce Viva
La Nuit Trésor
Eau de Parfum - La Vie est Belle
Chloé Eau de Parfum
Eau de Parfum - La petite Robe noire
Suddenly Diamonds
Mon Paris Eau de Parfum
La Belle
Hypnotic Poison Eau de Toilette
Allure

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何从子文本节点中提取文本-Selenium

如何使用python selenium从svg中提取文本

在Python中使用Selenium从网页中提取文本

如何通过Python使用Selenium从网页中提取文本$ 7.56

如何使用Selenium从先前插入的输入中提取文本?

如何从Selenium中的模型对话框中提取文本?

如何使用Selenium和Python从div类中提取文本

如何通过Java使用Selenium从元素中提取文本

如何使用 Selenium 和 Python 从 webelements 中提取文本

PYTHON + SELENIUM(铬):如何从当前URL中提取特定文本,然后使用提取的文本转到其他文本?

如何使用Selenium从父节点中提取文本,不包括子节点中的文本

如何使用Selenium检索WebElement的文本-Python

无法从 Webelement Selenium Python 打印文本

如何从Google趋势中提取标题/文本并通过Selenium和Python打印它们

Selenium / python:每次滚动后从动态加载的网页中提取文本

如何通过Java使用Selenium Webdriver从下拉列表中提取firstselected选项的文本

如何通过 Python 中的 Selenium Webdriver 从 HTML 标记的所有子项中提取文本

Selenium代码等待CSS类可用并在Python中提取文本

如何从HTML表中提取行数据并将其转换为Selenium和C#的文本

Python/Selenium - 如何从模态淡入淡出内容中提取文本?

如何使用Selenium在Angular 7应用程序中提取工具提示文本

如何使用Selenium和Python在html代码中提取特定文本

如何从通过 Selenium 提供的 html 中提取摘要值文本 $39

无法使用 selenium webdriver 在 span 标签中提取文本名称

在Java + Selenium WebDriver中提取选择的选项

在Selenium VBA中提取更改的XPath

如何从Selenium Webdriver元素中提取链接?

从href标签selenium webscraper中提取html

使用Python Selenium从范围中提取日期