Skip to content
Advertisement

Why selenium dont find tag and class

I recently started using selenium and I can’t solve one problem. When I use find_element_by_tag(class)_name or _css_selector raise error:

JavaScript

But the element I’m looking for exists.

For example:

JavaScript

Output:

JavaScript

But if I want to find “navbar-brand” class, I can do it. What’s the problem?

And if I use find_elements, return empty list.

P.S If somebody can advise some useful material for studying selenium it’ll be very helpful.

Advertisement

Answer

As per the documentation of selenium.webdriver.common.by implementation:

JavaScript

So, using find_element_by_class_name() you won’t be able to pass multiple class names. Passing multiple classes you will face an error.


Solution

To locate the element you can use either of the following Locator Strategies:

  • Using css_selector:

    JavaScript
  • Using xpath:

    JavaScript

Ideally, to locate the element you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    JavaScript
  • Using XPATH:

    JavaScript
  • Note : You have to add the following imports :

    JavaScript

References

You can find a couple of relevant detailed discussions in:

Advertisement