简体   繁体   English

使用 Selenium Python 在网页上找不到元素

[英]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 查找也不起作用:

在此处输入图片说明

在此处输入图片说明

then,然后,

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.

相关问题 无法使用 Selenium python 找到页面元素 - Can't find page elements using Selenium python Selenium Python 无法在带有单独滚动条的 web 页面的内部找到元素 - Selenium Python can't find elements in a inner part of a web page with a separated scroll bar 使用 Selenium 和 Python 查找网页上的所有元素 - Find all elements on a web page using Selenium and Python Selenium Python 库在登录页面上找不到元素 - Selenium Python library can't find elements on login page 找不到元素但它们存在 Selenium Python - Can't find elements but they exist Selenium Python python selenium,无法从page_source中找到元素,而可以从浏览器中找到 - python selenium, can't find elements from page_source while can find from browser 无法访问页面上的元素(Selenium Python) - Can't Access Elements On Page (Selenium Python) Python Selenium报废崩溃,我可以在网页的一部分中找到元素吗? - Python Selenium Scraping Crashes, Can I Find Elements For Part of The Web Page? Selenium(Python、Chrome)如何找到在开发工具中可见但在页面源代码中不可见的 web 元素? - How can Selenium (Python, Chrome) find web elements visible in dev tools, but not visible in page source? Python Selenium:在页面上找不到元素 - Python Selenium: Can't find an element on page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM