[英]Can't find elements on web page using Selenium Python
I want to find text element on the web page (address is below)我想在网页上找到文本元素(地址在下面)
I am interested in reddit post title.我对 reddit 帖子标题感兴趣。 (it is pointed out on the screen)
(屏幕上有提示)
I use Ctrl + Shift + I and inspected element and get:我使用Ctrl + Shift + I并检查元素并得到:
<h2 class="s1ua9il2-0 hsWiTe" data-redditstyle="true">The problems of a dutchman in China.</h2>
There is no id and name.没有 id 和 name。 Only tag ('h2') and class ("s1ua9il2-0 hsWiTe"), right?
只有标签('h2')和类(“s1ua9il2-0 hsWiTe”),对吗?
My code below doesn't work:我下面的代码不起作用:
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()
ERROR:错误:
selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: Compound class names not permitted
selenium.common.exceptions.InvalidSelectorException:消息:无效选择器:不允许复合类名
Also, finding by css_selector doesn't work too, when I use copy click:此外,当我使用复制单击时,通过 css_selector 查找也不起作用:
尝试通过 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.