【问题标题】:Python Selenium Webpage fill: To download data from linksPython Selenium 网页填充:从链接下载数据
【发布时间】:2017-10-11 13:21:10
【问题描述】:

我已编译此代码以从具有多个下载链接的网页执行下载迭代。单击下载链接后,网页会生成一个网络表单,必须填写并提交该表单才能开始下载。我已经尝试运行代码并在“try”和“except”块代码中遇到问题(错误:异常条款太宽泛),最后有一个与“提交”相关的错误(错误:方法提交可能是静态的)这两者随后都会导致“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()

【问题讨论】:

    标签: python selenium download autofill


    【解决方案1】:

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

    1 - “太宽泛的例外”这是一个警告,告诉您应该排除特定错误,而不是所有错误。在您的“除外”行中应该是 except [TheExceptionYouAreTreating]: 之类的示例,例如 except ValueError:。但是,这不应阻止您的代码运行

    2 - “错误:方法提交可能是静态的”这是警告,告诉您方法 submit 是静态方法(基本上是不使用 self 属性的方法)来抑制此警告,您可以像这样使用装饰器@staticmethod

    @staticmethod
    def submit():
        ...
    

    3 - “SyntaxError: invalid syntax” 这是阻止代码运行的原因。这是一个错误,告诉您代码中的某些内容有误。我认为这可能是您班级的缩进。试试这个:

    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

    【讨论】:

      猜你喜欢
      • 2018-02-22
      • 2018-03-26
      • 2022-01-13
      • 2014-06-21
      • 1970-01-01
      • 2017-01-30
      • 1970-01-01
      • 1970-01-01
      • 2019-02-16
      相关资源
      最近更新 更多