我想在网页上找到文本元素(地址在下面)enter image description here
我对reddit帖子标题感兴趣(它在屏幕上指出)
我使用 Ctrl + Shift + I 并检查元素并得到:
<h2 class="s1ua9il2-0 hsWiTe" data-redditstyle="true">The problems of a dutchman in China.</h2>
没有ID和名称。只有标签('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查找到: enter image description here