Reputation: 111
I want to find text element on the web page (address is below)
I am interested in reddit post title. (it is pointed out on the screen)
I use Ctrl+Shift+I and inspected element and get:
<h2 class="s1ua9il2-0 hsWiTe" data-redditstyle="true">The problems of a dutchman in China.</h2>
There is no id and name. Only tag ('h2') and class ("s1ua9il2-0 hsWiTe"), right?
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
Also, finding by css_selector doesn't work too, when I use copy click:
Upvotes: 0
Views: 928
Reputation: 2650
then,
from selenium.webdriver.common.by import By
driver.find_element(By.XPATH, '//button[text()="Some text"]')
Upvotes: 2
Reputation: 58
Try to do it by xpath f12 - click on element and copy xpath for example:
browser.find_element_by_xpath('//*[@id="modal-manager"]/div/div/div[2]/div/div[3]/div[1]/button')
Upvotes: 0