I am building a application that should help smaller local business to get google reviews from there customers easier. For that I am using Selenium in Python.
This is the procedure explained:
- In chrome i open the direct review link.
- The review link first loads the google maps page and then the pop-up window opens with the star rating selection at the top and below the review input box.
- I want with selenium to select the 5th star.
This is my current code:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
review_link = "https://g.page/r/CQcv68N5l734EB0/review"
# Set up the Chrome webdriver
driver = webdriver.Chrome()
driver.maximize_window()
# Load the review page and wait for it to load
driver.get(review_link)
time.sleep(10)
# Switch to the iframe containing the review stars
driver.switch_to.frame(driver.find_element(By.NAME, "goog-reviews-write-widget"))
time.sleep(5)
# Click the 5th star using its CSS selector
five_star = driver.find_element(By.CSS_SELECTOR, "#kCvOeb > div.O51MUd > div.Ugvple > div > div.WGh5lc.u4Qsab > div > div:nth-child(5)")
five_star.click()
# Switch back to the main page
driver.switch_to.default_content()
print("selected five stars")
# Close the webdriver
driver.quit()
Just as a note, i know that WebDriverWait()
is better than time.sleep()
. The reason i choose time.sleep()
is for simplicity and easier testing. In production i would change it to WebDriverWait()
. Same story with the css selector, i will change this in future to a simpler XPath.
With the code above i am getting this error:
selenium.common.exceptions.ElementClickInterceptedException:
Message: element click intercepted: Element <iframe name="goog-reviews-write-widget" role="presentation" class="goog-reviews-write-widget" src="https://www.google.com/maps/api/js/ReviewsService.LoadWriteWidget2?key=AIzaSyAQiTKe3tivKXammrJ6ov6u8E7KwZPNFss&authuser=0&hl
=en&origin=https%3A%2F%2Fwww.google.com&pb=!2m1!1sChIJselsQL2hmkcRBy_rw3mXvfg!7b1&cb=26368821"
cd_frame_id_="c0287439a9a949d63db71626600d7c1c"></iframe> is not clickable at point (780, 184).
Other element would receive the click: <iframe name="goog-reviews-write-widget" role="presentation" class="goog-reviews-write-widget" src="https://www.google.com/maps/api/js/ReviewsService.LoadWriteWidget2?key=AIzaSyAQiTKe3tivKXammrJ6ov6u8E7KwZPNFss&authuser=0&hl=en&origin=https%3A%2F%2Fwww.google.com&pb=!2m1!1sChIJselsQL2hmkcRBy_rw3mXvfg!7b1&cb=14671787"></iframe>
As I understand it, the Iframe intercepts the click. This is the selector of the 5th star, that i am using:
Why do i have to switch to Iframe anyways you ask maybe? This is because, if i don't switch to IFrame, the css selector to the 5th can't be found. Here is the IFrame i am referring to:
My attempts of solving this were:
Using the css selector of 5th star svg inside the div of the 5th star. Here I also got a
ElementClickInterceptedException
, but this the click was intercepted by the div in which the svg is inside and that i am clicking in the above code from me. Also the click didn't when trough as the 5th star wasn't selected.using javascript click method:
driver.execute_script("arguments[0].click();", five_star)
With this line instead of the .click()
method, the code went trough without any exceptions but the click didn't apply the 5th star.
I am now getting out of ideas how to solve this problem. How could I click on the 5th Star inside the Iframe?