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

Each of the search results "7-pack" here contains a number of reviews, for example. "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

      

Consistent with the other answers here, all I had to do was remove the space and try again. Bad luck - empty list

So I am trying find_element_by_css_selector()

:

find_elements_by_css_selector(".fl.r-iNTHbQvDybDU")

      

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

+3


source to share


4 answers


I wouldn't rely on autogenerated class names like these. Besides being unreliable, it makes your code less readable. Instead, get links that contain "overview" text.

Combined solution with Webdriver / Selenium: how to find an element when it doesn't have a class name, id or css selecector? thread:

import re

from selenium.common.exceptions import NoSuchElementException    
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver


driver = webdriver.Chrome()
driver.get('https://www.google.com/?gws_rd=ssl#q=plumbers%2BAvondale%2BAZ')

# waiting for results to load
wait = WebDriverWait(driver, 10)
box = wait.until(EC.visibility_of_element_located((By.ID, "lclbox")))

phone_re = re.compile(r"\(\d{3}\) \d{3}-\d{4}")

for result in box.find_elements_by_class_name("intrlu"):
    for span in result.find_elements_by_tag_name("span"):
        if phone_re.search(span.text):
            parent = span.find_element_by_xpath("../..")
            print parent.text
            break

    try:
        reviews = result.find_element_by_partial_link_text("review").text
    except NoSuchElementException:
        reviews = "0 Google reviews"

    print reviews
    print "-----"

      



Printing

360 N Central Ave
Avondale, AZ
(623) 455-6605
1 Google review
-----
Avondale, AZ
(623) 329-5170
4 Google reviews
-----
Tolleson, AZ
(623) 207-1995
7 Google reviews
-----
3947 N 146th Dr
Goodyear, AZ
(602) 446-6576
1 Google review
-----
564 W Western Ave
Goodyear, AZ
(623) 455-6605
0 Google reviews
-----
14190 W Van Buren St
Goodyear, AZ
(623) 932-5300
0 Google reviews
-----

      

+1


source


Try the following:



find_elements_by_css_selector(".r-iNTHbQvDybDU")

      

+1


source


How about this:

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

      

This assumes a tag for class = div

.

If it's something else - otherwise replace div w / with the appropriate tag.

+1


source


U need to add the tag name before it.

For example, it's inside a div element and then:

Selenium.find_element_by_class_name(div.ur.class.name)

      

0


source







All Articles