You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python中Selenium调用Chrome后无法加载URL的问题求助

问题:Chrome启动后无响应,driver.get(url)未发起请求

浏览器启动后无任何操作,driver.get(url)未发起请求,浏览器无响应。我怀疑问题与Chrome启动器有关,因需复用已登录谷歌账号的会话提交预填谷歌表单,必须使用完整Chrome实例。由于不知如何获取当前会话向/formresponse发送POST请求,故尝试直接使用已登录的Chrome实例,但出现上述问题。

原代码

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()

问题分析与修复方案

  1. ChromeService路径错误
    ChromeService需要指向ChromeDriver可执行文件,而非Chrome浏览器的chrome.exe。当前路径指向浏览器程序,导致Selenium无法建立驱动连接,浏览器仅能打开但无法被控制。需下载与Chrome版本匹配的ChromeDriver,将路径改为chromedriver.exe的位置。

  2. Profile加载参数错误
    使用user-data-dir时,应指向Chrome用户数据的根目录(C:\Users\myuser\AppData\Local\Google\Chrome\User Data),再通过--profile-directory参数指定具体的Profile名称(如Profile 3)。直接将user-data-dir设为单个Profile目录会导致Chrome配置加载异常。

  3. 元素定位方法语法错误
    Selenium Python中元素定位方法为driver.find_element(),而非Java风格的driver.findElement(),该错误会直接导致代码运行失败。

  4. 替换硬等待为显式等待
    硬编码的time.sleep稳定性差,改用WebDriverWait显式等待元素可交互后再操作,避免因页面加载延迟导致的元素找不到问题。


修正后的代码

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service as ChromeService
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

# 替换为你的ChromeDriver路径
chromedriver_path = r"C:\path\to\chromedriver.exe"

# Chrome用户数据根目录
chrome_user_data_dir = r"C:\Users\myuser\AppData\Local\Google\Chrome\User Data"
# 具体的Profile名称
chrome_profile_name = "Profile 3"

chrome_options = Options()
chrome_options.add_argument(f"user-data-dir={chrome_user_data_dir}")
chrome_options.add_argument(f"--profile-directory={chrome_profile_name}")

service = ChromeService(executable_path=chromedriver_path)

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

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)

# 显式等待提交按钮可点击
submit_button = WebDriverWait(driver, 20).until(
    EC.element_to_be_clickable((By.XPATH, "//span[text()='Enviar']"))
)
submit_button.click()

time.sleep(3)  # 短暂等待提交完成

driver.quit()

内容的提问来源于stack exchange,提问作者LautaroColella

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 02:58:13