0

For the below line of code:

driver.find_element(By.CLASS_NAME,'list-card-heading')

I am getting below error:

NoSuchElementException                    Traceback (most recent call last)
Input In [23], in <cell line: 1>()
1 driver.find_element(By.CLASS_NAME,'list-card-heading')

Initially I had :

driver.find_element_by_class_name('list-card-heading')

but did some modifications in code by adding this below two lines:

from selenium.webdriver.common.by import By
driver.find_element(By.CLASS_NAME,'list-card-heading')

I was expecting to get below results:

<selenium.webdriver.remote.webelement.WebElement (session="945ae2a8da0536bea44330c4bbf0b24e", element="581cbcf9-7bb9-40dd-8601-4109cad55272")

But got this error:

NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".list-card-heading"}

Is it the driver issue? or selenium library issue.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

2 Answers2

0

This line of code:

driver.find_element_by_class_name('list-card-heading')

in is equivalent to:

driver.find_element(By.CLASS_NAME,'list-card-heading')

using

Possibly the AUT(Application Under Test) changed or is a dynamic element, the reason you see the error.


This usecase

To identify a visible element ideally you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following locator strategies:

element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CLASS_NAME, "list-card-heading")))

Note : You have to add the following imports :

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • when I put this- element= I am getting TimeoutException-chk below: --------------------------------------------------------------------------- TimeoutException Traceback (most recent call last) Input In [12], in () ----> 1 element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CLASS_NAME, "list-card-heading"))) File ~\anaconda3\inst\lib\site-packages\selenium\webdriver\support\wait.py:80, in WebDriverWait.until(self, method, message) 78 if time.time() > end_time: 79 break – user20660075 Feb 08 '23 at 04:47
0

A NoSuchElementException is thrown when the webdriver cannot find an elements that use the locator strategy it has been given.

While sometimes this happens because of a legitimately bad xpath, in my experience, this often happens because of a dom timing issue, and this can be caused by a variety of things, either in your code, or on the website's side of things.

Solution

I recommend using a WebDriverWait to make sure an element exists before using attempting to interact with it. Here is an example of what that might look like:

//Declare your driver as you normally would
WebDriverWait wait = WebDriverWait(driver, 10)
wait.until(len(driver.find_elements(By.CLASS_NAME,'list-card-heading')) != 0)
driver.find_element(By.CLASS_NAME,'list-card-heading')
Josh Heaps
  • 296
  • 2
  • 13