Python Selenium 网页填充:从链接下载数据

Python Selenium Webpage fill: To download data from links

我编译了这段代码以从具有多个下载 link 的网页执行下载迭代。单击下载 link 后,网页会生成一个网络表单,必须填写并提交该表单才能开始下载。我已经尝试 运行 代码并在 'try'& 'except' 块代码中遇到问题(错误:异常条款太宽泛)并且最后有一个与 [=16 相关的错误=](错误:方法提交可能是静态的)这两个结果随后都会导致 'SyntaxError: invalid syntax '。任何建议/帮助将不胜感激。谢谢你。

import os
from selenium import webdriver

fp = webdriver.FirefoxProfile()

fp.set_preference("browser.download.folderList",2)
fp.set_preference("browser.download.manager.showWhenStarting",False)
fp.set_preference("browser.download.dir", os.getcwd())
fp.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/x-msdos-program")

driver = webdriver.Firefox(firefox_profile=fp)
driver.get('http://def.com/catalog/attribute')
#This is to find the download links in the webpage one by one
i=0
while i<1:
    try:
        driver.find_element_by_xpath('//*[@title="xml (Open in a new window)"]').click()
    except:
        i=1
#Once the download link is clicked this has to fill the form for submission which fill download the file

        class FormPage(object):
            def fill_form(self, data):
                driver.find_element_by_xpath('//input[@type = "radio" and @value = "Non-commercial"]').click()
                driver.find_element_by_xpath('//input[@type = "checkbox" and @value = "R&D"]').click()
                driver.find_element_by_xpath('//input[@name = "name_d"]').send_keys(data['name_d'])
                driver.find_element_by_xpath('//input[@name = "mail_d"]').send_keys(data['mail_d'])

                return self

            def submit(self):
                driver.find_element_by_xpath('//input[@value = "Submit"]').click()
                data = {
                    'name_d': 'abc',
                    'mail_d': 'xyz@gmail.com',
                }
                FormPage().fill_form(data).submit()
                driver.quit()

实际上你有两个警告和一个错误:

1 - "Too broad exception" 这是一个警告,告诉您应该排除特定错误,而不是所有错误。在您的 "except" 行中,应该类似于 except [TheExceptionYouAreTreating]:,例如 except ValueError:。但是,这不应阻止您的代码从 运行

2 - "Error: method submit maybe static" 这是警告告诉你方法 submit 是一个静态方法(基本上是一个不使用 self 属性的方法)来抑制这个警告你可以像这样使用装饰器 @staticmethod

@staticmethod
def submit():
    ...

3 - "SyntaxError: invalid syntax" 这就是阻止您的代码来自 运行 的原因。这是一个错误,告诉您代码中写错了。我认为这可能是您 class 上的缩进。试试这个:

i=0
while i<1:
    try:
        driver.find_element_by_xpath('//*[@title="xml (Open in a new window)"]').click()
    except:
        i=1

#Once the download link is clicked this has to fill the form for submission which fill download the file
class FormPage(object):
    def fill_form(self, data):
        driver.find_element_by_xpath('//input[@type = "radio" and @value = "Non-commercial"]').click()
        driver.find_element_by_xpath('//input[@type = "checkbox" and @value = "R&D"]').click()
        driver.find_element_by_xpath('//input[@name = "name_d"]').send_keys(data['name_d'])
        driver.find_element_by_xpath('//input[@name = "mail_d"]').send_keys(data['mail_d'])

        return self

    def submit(self):
        driver.find_element_by_xpath('//input[@value = "Submit"]').click()
        data = {
            'name_d': 'abc',
            'mail_d': 'xyz@gmail.com',
        }
        FormPage().fill_form(data).submit()
        driver.quit()

还有一件事。这些都是非常简单的错误和警告,您应该能够通过仔细阅读错误的内容自行修复它们。我还建议您阅读有关 Exceptions

的内容