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)
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)
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.
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.