0

What's the problem?

Im currently trying to scrape data from a subreddit (I am using the old-reddit chrome-extension that gives back the old look of reddit -> this way it's easier to scrape), but whenever I'm trying to get the results I get the error from this little bit of code:

xpath = "//a[@class='title may-blank loggedin ']"
element = driver.find_element_by_xpath(xpath)

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//a[@class='title may-blank loggedin ']"}

What did I try to fix the problem?

I already saw many posts with similar errors, often related with scraping the results before the page was loaded. I tried to fix that with:

time.sleep(20)

But still no diffrence.

The path is correct as well. I entered the same path on Chrome's console and it displayed correct results.

When I search for tag names, class names etc., I get correct results as well.

Thank you for your help in advance!!

Stack trace

Traceback (most recent call last):
  File "D:\Web_Dev\Projekte\cs50_project\test.py", line 68, in <module>
    main()
  File "D:\Web_Dev\Projekte\cs50_project\test.py", line 32, in main
    element = driver.find_element_by_xpath(xpath)
  File "C:\Users\User\AppData\Roaming\Python\Python39\site-packages\selenium\webdriver\remote\webdriver.py", line 394, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
  File "C:\Users\User\AppData\Roaming\Python\Python39\site-packages\selenium\webdriver\remote\webdriver.py", line 976, in find_element
    return self.execute(Command.FIND_ELEMENT, {
  File "C:\Users\User\AppData\Roaming\Python\Python39\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\User\AppData\Roaming\Python\Python39\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response    
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//a[@class='title may-blank loggedin ']"}
  (Session info: chrome=88.0.4324.150)
9
  • Try it with xpath = "//a[@class='title.may-blank.loggedin ']" or just xpath = "//a[@class='loggedin ']" Commented Feb 6, 2021 at 11:48
  • If this doesnt work, could you give us the url? Commented Feb 6, 2021 at 11:49
  • Yes it its reddit.com/search/?q=cs50 but I am using a chrome extension that changes the layout of the page chrome.google.com/webstore/detail/old-reddit-redirect/… Commented Feb 6, 2021 at 13:51
  • I am not sure if it works with the extention, I can just help you without the extention, so what to you need from the page? Commented Feb 6, 2021 at 13:54
  • I have tried both of your proposals but neither works unfortunately, I am stil getting the same error Commented Feb 6, 2021 at 13:54

2 Answers 2

1

i would try to multiply classes in xpath, like that:

    xpath = "//a[@class='title'][@class='may-blank'][@class='loggedin']"
    element = driver.find_element_by_xpath(xpath)

or like that:

    xpath = "//a[@class='title' and @class='may-blank' and @class='loggedin']"
    element = driver.find_element_by_xpath(xpath)
Sign up to request clarification or add additional context in comments.

Comments

0

What happened?

Calling this XPath "//a[@class='title may-blank loggedin ']" does not work for Selenium because the space is like a delimiter for classes in HTML so you are looking for a class which cant exsist.

How to solve it?

Solving this issue is very simple just put dots instead of delimiter, to show that you look for an element which has all three classes like this:

    xpath = "//a[@class='title.may-blank.loggedin']"
    element = driver.find_element_by_xpath(xpath)

3 Comments

IMO, xpath = "//a[@class='title.may-blank.loggedin']" isn't a valid xpath.
When I look at your profile it seems like you know a little bit about Selenium, so you are right but why does it work for him and what would be the right answer?
Would this be the soloution? //div[contains(@class, 'class1') and contains(@class, 'class2')] got it from here stackoverflow.com/questions/3881044/…

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.