Search code examples
pythonselenium-webdrivergoogle-forms

Selenium in python doesn't open chrome url


When the browser opens it just stays put and doesn't act. The driver.get(url) doesn't make a request, nothing happens in the browser.

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service as ChromeService
import time

chrome_executable_path = r"C:\Program Files\Google\Chrome\Application\chrome.exe"

# path to the chrome profile to get the logged google acc
chrome_profile_dir = r"C:\Users\myuser\AppData\Local\Google\Chrome\User Data\Profile 3"

# use the chrome profile
chrome_options = Options()
chrome_options.add_argument(f"user-data-dir={chrome_profile_dir}")

service = ChromeService(executable_path=chrome_executable_path)

driver = webdriver.Chrome(service=service, options=chrome_options)

# this doesn't work, but it's not related to the url/nothing after this
url = "https://docs.google.com/forms/d/e/myformid/viewform?usp=pp_url&entry.1824=abc&entry.20988=def&entry.1589=123"
driver.get(url)

time.sleep(10)

submit_button = driver.findElement(By.xpath("//span[text()='Enviar']"))
submit_button.click()

# wait for form submission
time.sleep(10)

driver.quit()

I believe it's related to the chrome launcher. I have to use the full chrome instance because I need to share the google session to be able to send the google form using my google account.

What I'm attempting is to submit a prefilled google form. I don't know how to get my current session to make the post request to /formresponse so I figured I would directly use the instance that is already logged in.


Solution

  • Few issues in your code:

    1. executable_path argument accepts the path for chromedriver.exe and not chrome.exe.

    Below line is incorrect

    chrome_executable_path = r"C:\Program Files\Google\Chrome\Application\chrome.exe"
    

    It should be something like:

    chrome_executable_path = r"C:\full path\chromedriver.exe"
    

    Having said the above, you can always use Selenium Manager, where you do not have to worry about setting the driver manually. Below answer should help:

    https://stackoverflow.com/a/76463081/7598774

    1. Below line has syntax error
    submit_button = driver.findElement(By.xpath("//span[text()='Enviar']"))
    

    It should be:

    submit_button = driver.find_element(By.XPATH, "//span[text()='Enviar']")
    
    1. Last one, below import statement is missing in your code:
    from selenium.webdriver.common.by import By