0

I want to find text element on the web page (address is below)

enter image description here

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:

enter image description here

2

2 Answers 2

2

enter image description here

then,

from selenium.webdriver.common.by import By

driver.find_element(By.XPATH, '//button[text()="Some text"]')
Sign up to request clarification or add additional context in comments.

4 Comments

I am very sorry, but this method doesn't work at next case: driver.get("youtube.com/watch?v=D1l_l_dP99Y") elem = driver.find_element(By.XPATH,'//*[@id="owner-name"]/a') # element is name of the channel "MrVinylObsessive" There is working video
You have to use elem.getText(). If you did not miss that, what error you are getting?
I miss that. Where should I use elem.getText().?
This way to get xpath helped me so much. just want to say that some websites change id of tags dinamicaly
0

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')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.