Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webdriver: How to find elements when class name contains space?

Each of the "7-pack" search results here contains a number of reviews e.g. "5 reviews", No reviews" etc.

The class name for each is fl r-iNTHbQvDybDU. It contains a space, so if I try find_elements_by_class_name(), I get:

InvalidSelectorError: Compound class names not permitted

According to other answers on here, all I needed to do was remove the space and retry. No luck - an empty list

So I try find_element_by_css_selector():

find_elements_by_css_selector(".fl.r-iNTHbQvDybDU")

Still no luck - empty list. What would you try next?

like image 941
Pyderman Avatar asked Jun 26 '15 03:06

Pyderman


People also ask

Can we find element based on class name using Selenium?

Selenium By.This method makes it possible to locate an element by referencing its class name. The class() method is found inside the By class of the Selenium WebDriver JavaScript library. The class also contains other alternative methods for locating elements. let loginBtn = await driver.

What is the best way to locate elements in Selenium?

ID locator in Selenium is the most preferred and fastest way to locate desired WebElements on the page. ID Selenium locators are unique for each element in the DOM. Since IDs are unique for each element on the page, it is considered the fastest and safest method to locate elements.

Can HTML class names have spaces?

A class name can't have spaces. When you have a space-separated string in your class attribute, you're always giving your element several classes.


1 Answers

How about this:

browser.find_elements_by_css_selector("div[class='fl r-iNTHbQvDybDU']")

This assumes tag for the class = div.

If it is something else - otherwise replace div w/ the appropriate tag..

like image 128
Moe Khan Avatar answered Sep 21 '22 00:09

Moe Khan