Search code examples
pythonseleniumweb-scrapingxpathwebdriverwait

Change Google Maps review sort with Selenium



I am facing interesting issue with my **Web Scraping** use case. I need to get newest **Google Maps reviews**.
I want to sort reviews by latest date. And all tutorials I am watching is in English, but in my native language the UI is different than in those tutorials.
I am able to click on the button using **Selenium** and button's **XPATH**, but I don't know how to change the sorting options from the visible drop menu.
# Click the sort button
driver.find_element_by_xpath('//*[@id="pane"]/div/div[1]/div/div/div[2]/div[8]/button').click()

Chrome inspection

select_by_visible_text() and select_by_value() doesn't work for me as I cannot select the button and doesn't work on div.

URL I am using : Link
To see my UI change to LITHUANIAN language.


Solution

  • First of all you have to learn how to create correct XPath locators.
    Long XPath expressions are too fragile.
    The "Sort Reviews" button locator instead of
    //*[@id="pane"]/div/div[1]/div/div/div[2]/div[8]/button can be
    //button[@aria-label='Sort reviews'] or
    //button[@data-value='Sort']
    After clicking this button, to sort reviews by latest date you can click this element: //li[@data-index='1']
    So basically this will work:

    driver.find_element_by_xpath("//li[@data-index='1']").click()
    

    But since you need to wait for dialog to open after clicking the Sort button you need to utilize Expected Condition wait, as following:

    wait = WebDriverWait(driver, 20)
    wait.until(EC.visibility_of_element_located((By.XPATH, "//li[@data-index='1']"))).click()
    

    You will need the following imports for this:

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