我试图在此页面上获得所有香水的名称
但是,当我尝试获取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] 删除。
我来说两句