【发布时间】:2021-02-01 03:43:27
【问题描述】:
已编辑:我合并了 Sushil 建议的最后几行。最后,我在终端中复制输出。我仍然没有得到压缩文件。
已解决:我的错误是由于驱动程序和 chrome 版本之间的不兼容造成的。我按照此处的说明进行了修复:unknown error: call function result missing 'value' for Selenium Send Keys even after chromedriver upgrade
我正在尝试使用 Selenium 填写表格并下载 zip 文件。 经过广泛的谷歌搜索,我编写了一个 Python 代码,但我目前无法下载该文件。浏览器打开,但没有填写任何内容。
我是 Python 的新手,所以我猜我错过了一些非常琐碎的事情,因为我试图从中获取信息的网站非常简单。
这是我尝试过的:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome(executable_path='/home/miranda/webscrap_Python/chromedriver')
#driver.wait = WebDriverWait(driver,5)
driver.get("http://www.cis.es/cis/opencms/EN/formulario.jsp?dwld=/Microdatos/MD3288.zip")
Name = '//*[@id="Nombre"]'
LastName = '//*[@id="Apellidos"]'
University = '//*[@id="profesion"]'
email = '//*[@id="Email"]'
ob_req = '//*[@id="objeto1"]'
terms = '//*[@id="Terminos"]'
download = '//*[@id="mediomicrodatos"]/form/div[3]/input'
driver.find_element_by_xpath(Name).send_keys("Miranda")
driver.find_element_by_xpath(LastName).send_keys("MyLastName")
driver.find_element_by_xpath(University).send_keys("MySchool")
driver.find_element_by_xpath(email).send_keys("my_email@gmail.com")
#lines added by Sushil:
ob_req_element = driver.find_element_by_xpath(ob_req) #Finds the ob_req element
driver.execute_script("arguments[0].click();", ob_req_element) #Scrolls down to the element and clicks on it
terms_element = driver.find_element_by_xpath(terms) #The same is repeated here
driver.execute_script("arguments[0].click();", terms_element)
driver.find_element_by_xpath(download).click() #Scrolling down is not needed for the download button as it would already be in view. Only when an element is not in view should we scroll down to it in order to click on it.
在我的终端中输出:
Traceback (most recent call last):
File "myCode.py", line 18, in <module>
driver.find_element_by_xpath(Name).send_keys("Miranda")
File "/home/miranda/anaconda3/lib/python3.7/site-packages/selenium/webdriver/remote/webelement.py", line 479, in send_keys
'value': keys_to_typing(value)})
File "/home/miranda/anaconda3/lib/python3.7/site-packages/selenium/webdriver/remote/webelement.py", line 633, in _execute
return self._parent.execute(command, params)
File "/home/miranda/anaconda3/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/home/miranda/anaconda3/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: call function result missing 'value'
(Session info: chrome=86.0.4240.75)
(Driver info: chromedriver=2.29.461571 (8a88bbe0775e2a23afda0ceaf2ef7ee74e822cc5),platform=Linux 5.4.0-45-generic x86_64)
【问题讨论】:
-
谢谢。我已经非常仔细地阅读了终端中的输出,但我发现它非常混乱。通常,终端非常有用,因为它指示有问题的线路。这一次,终端引用了我的代码中甚至没有的行,所以我不知道如何解决这个问题。终端只提到了两次错误:self.error_handler.check_response(response);和 elenium.common.exceptions.WebDriverException:消息:未知错误:调用函数结果缺少“值”。我一直在谷歌上搜索它们,但到目前为止我仍在苦苦挣扎。
-
您在帖子中写道浏览器打开,但没有填写任何内容。也有错误?
-
@AMC,对不起,我不确定你的意思。我通常使用 R,当我尝试在 R 中执行此过程时,我可以打开页面并填写表格。我的意思是,在 Python 中我只能打开选项卡,但无法成功填写表单。
-
如果您收到错误消息,请更新您的问题。
标签: python selenium selenium-webdriver web-scraping