我想在網(wǎng)頁上找到文本元素(地址在下面)我對 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 seleniumfrom selenium import webdriverfrom selenium.webdriver.common.keys import Keysoptions = 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 查找也不起作用:
2 回答

慕俠2389804
TA貢獻1719條經(jīng)驗 獲得超6個贊
嘗試通過 xpath f12 來完成 - 單擊元素并復制 xpath 例如:
browser.find_element_by_xpath('//*[@id="modal-manager"]/div/div/div[2]/div/div[3]/div[1]/button')
添加回答
舉報
0/150
提交
取消