1

I am new to webscraping & not a developer nor have any html exp. and was trying to pull some details from my account after logging in into the website but getting errors in find_element_by_class_name()

this is the code I have tried:

from selenium import webdriver

driver = webdriver.Chrome('path/chromedriver.exe')
driver.get("https://www.URL.COM") 

# logged into account manually & maneuvered to the page manually

driver.find_element_by_class_name('css-901oao css-cens5h r-1khnkhu r-13awgt0 r-1oke55r r-1enofrn r-1wzrnnt')

Error

---------------------------------------------------------------------------
NoSuchElementException                    Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_14516/1270707966.py in <module>
----> 1 driver.find_element_by_class_name('css-901oao css-cens5h r-1khnkhu r-13awgt0 r-1oke55r r-1enofrn r-1wzrnnt')

C:\ProgramData\Miniconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py in find_element_by_class_name(self, name)
    562             element = driver.find_element_by_class_name('foo')
    563         """
--> 564         return self.find_element(by=By.CLASS_NAME, value=name)
    565 
    566     def find_elements_by_class_name(self, name):

Also tried

driver.find_element_by_css_selector('css-901oao css-cens5h r-1khnkhu r-13awgt0 r-1oke55r r-1enofrn r-1wzrnnt')

From inspect I was able to view this & tried to extract the one highlighted in the image:

enter image description here

ViSa
  • 1,563
  • 8
  • 30

1 Answers1

1

class name expect a single class name. where as you are passing multiple class name here

css-901oao css-cens5h r-1khnkhu r-13awgt0 r-1oke55r r-1enofrn r-1wzrnnt

It won't work.

Instead

remove the spaces and make a CSS selector out of it.

driver.find_element(By.CSS_SELECTOR, ".css-901oao.css-cens5h.r-1khnkhu.r-13awgt0.r-1oke55r.r-1enofrn.r-1wzrnnt")

Also, Please remember find_element_by_class_name have been deprecated in newest selenium. You should use this instead

find_element(By.CLASS_NAME, "class name")

having said this, the locator that you've right now looks brittle in nature. Please use static attribute values.

You could try this xpath

//div[starts-with(@class,'css')]//div[@dir='auto' and contains(@style,'-webkit-line-clamp')]

Please check in the dev tools (Google chrome) if we have unique entry in HTML DOM or not.

xpath that you should check :

//div[starts-with(@class,'css')]//div[@dir='auto' and contains(@style,'-webkit-line-clamp')]

Steps to check:

Press F12 in Chrome -> go to element section -> do a CTRL + F -> then paste the xpath and see, if your desired element is getting highlighted with 1/1 matching node.

cruisepandey
  • 28,520
  • 6
  • 20
  • 38
  • When I try `driver.find_element_by(` I get an error `AttributeError: 'WebDriver' object has no attribute 'find_element_by'`. So I tried `driver.find_element(By.CSS_SELECTOR, ".css-901oao.css-cens5h.r-1khnkhu.r-13awgt0.r-1oke55r.r-1enofrn.r-1wzrnnt")` but then I get error: `NameError: name 'By' is not defined` – ViSa Nov 21 '21 at 11:49
  • My bad man ! there was a typo, I corrected it. Please try now. – cruisepandey Nov 21 '21 at 11:50
  • yes, even I thought its probably a typo so I tried `driver.find_element(By.CSS_SELECTOR, ".css-901oao.css-cens5h.r-1khnkhu.r-13awgt0.r-1oke55r.r-1enofrn.r-1wzrnnt")` but then I get error: `NameError: name 'By' is not defined`. Do i need to define `By` somewhere ??? – ViSa Nov 21 '21 at 11:54
  • 1
    Please import `from selenium.webdriver.common.by import By` – cruisepandey Nov 21 '21 at 11:57
  • Good evening to you too but I guess I will be spending my evening scratching my head on this as I have not got any data in this. On doing this `table = driver.find_element(By.CSS_SELECTOR, ".css-901oao.css-cens5h.r-1khnkhu.r-13awgt0.r-1oke55r.r-1enofrn.r-1wzrnnt")` I get `` & if I try `results = [] for tag in table: results.append(tag.text) print(results)` then I get `TypeError: 'WebElement' object is not iterable` – ViSa Nov 21 '21 at 12:12
  • When we use `find_element` it returns a single web element, so in your case it would be `table`, now to extract text just use `table.text` or `table.get_attribute('innerText')` – cruisepandey Nov 21 '21 at 12:14
  • 1
    In case of iteration like you are doing there `for tag in table:`, it should be `find_elements` not `find_element` – cruisepandey Nov 21 '21 at 12:15
  • 1
    by using `find_elements` it worked for me. thanks alot for staying here & helping me out through this. Really really Appreciate that :) – ViSa Nov 21 '21 at 12:18