当浏览器打开时,它只是停留在原地不动,不起作用.driver.get(url)不会发出请求,浏览器中什么都不会发生.

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

我相信这和 chromium 合金emits 器有关.我必须使用完整的Chrome实例,因为我需要共享Google会话才能使用我的Google帐户发送Google表单.

我try 的是提交一份预填好的谷歌表格.我不知道如何让我的当前会话向/formResponse发出POST请求,所以我想我应该直接使用已经登录的实例.

推荐答案

代码中有几个问题:

  1. executable_path参数接受chromedriver.exe而不是chrome.exe的路径.

下面的线不正确

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

它应该是这样的:

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

话虽如此,你总是可以使用Selenium Manager,在那里你不必担心手动设置驱动程序.下面的答案应该会有所帮助:

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

  1. 下面的行有语法错误
submit_button = driver.findElement(By.xpath("//span[text()='Enviar']"))

它应该是:

submit_button = driver.find_element(By.XPATH, "//span[text()='Enviar']")
  1. 最后一条,您的代码中缺少以下导入语句:
from selenium.webdriver.common.by import By

Python相关问答推荐

2维数组9x9,不使用numpy.数组(MutableSequence的子类)

将整组数组拆分为最小值与最大值之和的子数组

为什么tkinter框架没有被隐藏?

从一个系列创建一个Dataframe,特别是如何重命名其中的列(例如:使用NAs/NaN)

我想一列Panadas的Rashrame,这是一个URL,我保存为CSV,可以直接点击

导入...从...混乱

将JSON对象转换为Dataframe

删除marplotlib条形图上的底边

需要帮助重新调整python fill_between与数据点

如何更改groupby作用域以找到满足掩码条件的第一个值?

OpenCV轮廓.很难找到给定图像的所需轮廓

python sklearn ValueError:使用序列设置数组元素

统计numpy. ndarray中的项目列表出现次数的最快方法

Js的查询结果可以在PC Chrome上显示,但不能在Android Chrome、OPERA和EDGE上显示,而两者都可以在Firefox上运行

在Django中重命名我的表后,旧表中的项目不会被移动或删除

如何设置nan值为numpy数组多条件

如何获得满足掩码条件的第一行的索引?

如何在Django查询集中生成带有值列表的带注释的字段?

使用Django标签显示信息

牛郎星直方图中分类列的设置顺序