Search code examples
pythoncsspython-2.7selenium-webdriverwebdriver

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?


Solution

  • I would not rely on the auto-generated class names like these. Aside from being non-reliable, it is making your code less readable. Instead, get the links containing "review" text.

    Combined solution with the Webdriver/Selenium: How to find element when it has no 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 "-----"
    

    Prints:

    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
    -----