【发布时间】: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