简体   繁体   中英

Python selenium for write review google maps (selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: invalid locator )

Goal:

  • Write a review of a location on google maps with python and selenium

Stages:

  • Login to google account (already successful)
  • Go to the review page (already successful)
  • Giving 5 stars (not yet successful)
  • Give a review (not yet successful)
  • Submit a review (unsuccessful)

Problem:

  • An error occurred: selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: invalid locator (Session info: chrome=108.0.5359.125)
  • Stuck on the page in the following image在此处输入图像描述

I've checked inspect to make sure在此处输入图像描述

Code:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from time import sleep
chrome_options = webdriver.ChromeOptions()
driver = webdriver.Chrome(executable_path=r'C:\Users\user\OneDrive\Desktop\selenium\chromedriver.exe', options=chrome_options)
# go to login page
driver.get("https://accounts.google.com/login?hl=id")
# write the email address
email = driver.find_element("name", "identifier")
email.send_keys("email@gmail.com")
sleep(2)
email.send_keys(Keys.RETURN)
sleep(5)
# write the password
password = driver.find_element("name", "Passwd")
password.send_keys("Password")
password.send_keys(Keys.RETURN)
sleep(5)
# go to review page
driver.get("https://search.google.com/local/writereview?placeid=ChIJDf8ln6V5ei4REzngJl6HEEc&pli=1")
sleep(8)
driver.find_element("aria-label", "Lima bintang").click()

How to solve this problem? At least to click the 5 stars. Thanks

That elements block is inside an iframe. So, you need to switch into the iframe to access those elements. So, you code can be as following:

driver.get("https://search.google.com/local/writereview?placeid=ChIJDf8ln6V5ei4REzngJl6HEEc&pli=1")
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it(driver.find_element(By.XPATH, "//div[not(@aria-hidden)]/iframe[@name='goog-reviews-write-widget']")))
driver.find_element("aria-label", "Lima bintang").click()

When finished working inside that iframe do not forget to get out to the default content with

driver.switch_to.default_content()

if you want to continue your test flow.
Also, instead of all these hardcoded pauses you should use WebDriverWait expected_conditions explicit waits.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

Related Question selenium.common.exceptions.InvalidArgumentException: Message: invalid argument error invoking get() using Selenium Webdriver through Python Is CssSelector the only way I can use for shadowroot? selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: invalid locator selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: missing 'cookie' while adding cookies using selenium webdriver selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: missing 'ELEMENT' error when switching frame in Selenium selenium.common.exceptions.InvalidArgumentException: Message: invalid argument error invoking get() with urls read from text file with Selenium Python URL must be a string selenium - selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: 'url' must be a string selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: File not found error while uploading file using Selenium Python selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: 'using' must be a string using waits and expected conditions selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: File not found error invoking send_keys() using Selenium selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: user data directory is already in use error with real Chrome Browser
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM