I am writing an automatic scoring tool in Google Maps. I managed to get it to the scoring tab, but I cannot give 5 stars there and press the send button. There is a problem in giving 5 stars.
Hre is my code block:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from webdriver_manager.chrome import ChromeDriverManager
import os
import time
def google_maps_comment_with_existing_profile(comment_text):
# Kullanıcı verileri ve profil yolu
user_data_dir = os.path.expanduser('~') + "\AppData\Local\Google\Chrome\User Data"
profile = "Profile 1" # veya "Profile 1", "Profile 2" gibi
# Chrome seçeneklerini ayarlama
chrome_options = Options()
chrome_options.add_argument(f"user-data-dir={user_data_dir}")
chrome_options.add_argument(f"profile-directory={profile}")
chrome_options.add_argument("--start-maximized")
# Chrome'u başlatma
service = Service(ChromeDriverManager().install())
browser = webdriver.Chrome(service=service, options=chrome_options)
try:
# Google Maps'te mekana git
browser.get("https://www.google.com/maps/place/Aspava+D%C3%BCr%C3%BCm+D%C3%B6ner/@37.0739082,37.3154215,13.75z/data=!4m6!3m5!1s0x1531e1a012adbbd3:0xb0211d1d7239889d!8m2!3d37.072678!4d37.3392742!16s%2Fg%2F1tgldpqs?entry=ttu")
time.sleep(5)
# Yorumlar sekmesine tıkla
reviews_tab = WebDriverWait(browser, 10).until(
EC.element_to_be_clickable((By.XPATH, '//button[contains(., "Yorumlar")]'))
)
reviews_tab.click()
time.sleep(3)
# Yorum yaz butonunu bul ve tıkla
write_review_button = WebDriverWait(browser, 10).until(
EC.element_to_be_clickable((By.XPATH, '//button[contains(., "Yorum yaz")]'))
)
write_review_button.click()
# iframe'e geçiş yap
iframe = WebDriverWait(browser, 10).until(
EC.presence_of_element_located((By.XPATH, '//iframe[@name="goog-reviews-write-widget"]'))
)
browser.switch_to.frame(iframe)
# Yıldız derecelendirmesi ve yorum yazma alanının görünür olmasını bekle
star_xpath = '//div[@aria-label="Puanlama yıldızları"]//div[@aria-label="Beş yıldız"]'
comment_box_xpath = '//textarea[@aria-label="Yorumunuzu yazın"]'
# Derecelendirme yıldızları ve yorum yazma alanının görünür olmasını bekle
WebDriverWait(browser, 10).until(
EC.visibility_of_element_located((By.XPATH, star_xpath))
)
WebDriverWait(browser, 10).until(
EC.visibility_of_element_located((By.XPATH, comment_box_xpath))
)
# Beş yıldız derecelendirmesine tıklama
star_rating = WebDriverWait(browser, 10).until(
EC.element_to_be_clickable((By.XPATH, star_xpath))
)
browser.execute_script("arguments[0].click();", star_rating)
print("Beş yıldız derecelendirmesine tıklandı.")
# Yorum alanına yorum yaz
comment_box = WebDriverWait(browser, 10).until(
EC.presence_of_element_located((By.XPATH, comment_box_xpath))
)
comment_box.send_keys(comment_text)
time.sleep(2)
# Yorum gönder
post_button = WebDriverWait(browser, 10).until(
EC.element_to_be_clickable((By.XPATH, '//button[@data-action-type="post"]'))
)
post_button.click()
time.sleep(5)
print("Yorum başarıyla gönderildi.")
except Exception as e:
print(f"Yorum gönderme sırasında hata: {e}")
finally:
# Tarayıcıyı kapatmadan önce kullanıcıdan onay al
input("İşlem tamamlandı. Tarayıcıyı kapatmak için Enter'a basın...")
browser.quit()
# Gönderilecek yorum
comment = "Harika bir yer, kesinlikle tavsiye ederim!"
# Yorumları gönder
google_maps_comment_with_existing_profile(comment)
When I examined the page, I noticed that an iframe opened when I clicked on the comment section and I acted accordingly, but I failed.
New contributor
Karambol is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1