簡體   English   中英

使用 Selenium Python 在網頁上找不到元素

[英]Can't find elements on web page using Selenium Python

我想在網頁上找到文本元素(地址在下面)

在此處輸入圖片說明

我對 reddit 帖子標題感興趣。 (屏幕上有提示)

我使用Ctrl + Shift + I並檢查元素並得到:

<h2 class="s1ua9il2-0 hsWiTe" data-redditstyle="true">The problems of a dutchman in China.</h2>

沒有 id 和 name。 只有標簽('h2')和類(“s1ua9il2-0 hsWiTe”),對嗎?

我下面的代碼不起作用:

import selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

options = webdriver.ChromeOptions()
options.add_argument('headless')
driver = webdriver.Chrome(r"C:\Users\vishniakov\Desktop\python bj\driver\chromedriver.exe",chrome_options=options)
driver.get("https://www.reddit.com/r/funny/comments/9kxrv6/the_problems_of_a_dutchman_in_china/")
print(driver.title)
elem = driver.find_element_by_class_name("s1ua9il2-0 hsWiTe")
#driver.quit()

錯誤:

selenium.common.exceptions.InvalidSelectorException:消息:無效選擇器:不允許復合類名

此外,當我使用復制單擊時,通過 css_selector 查找也不起作用:

在此處輸入圖片說明

在此處輸入圖片說明

然后,

from selenium.webdriver.common.by import By

driver.find_element(By.XPATH, '//button[text()="Some text"]')

嘗試通過 xpath f12 來完成 - 單擊元素並復制 xpath 例如:

browser.find_element_by_xpath('//*[@id="modal-manager"]/div/div/div[2]/div/div[3]/div[1]/button')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM