2017-10-11 25 views
1

여러 다운로드 링크가있는 웹 페이지에서 다운로드 반복을 수행하기 위해이 코드를 컴파일했습니다. 다운로드 링크를 클릭하면 웹 페이지가 생성되어 다운로드를 위해 제출하고 제출해야합니다. 나는 '시도'에서 코드 및 얼굴 문제를 실행 해 보았습니다. & '예외'블록 코드 (오류 : 너무 광범위한 예외 조항)와 끝에 '제출'과 관련된 오류가 있습니다 (오류 : 둘 다 결과적으로 'SyntaxError : invalid syntax'가 발생합니다. 모든 제안/도움을 많이 주시면 감사하겠습니다. 고맙습니다.Python Selenium 웹 페이지 채우기 : 링크에서 데이터를 다운로드하려면

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': '[email protected]', 
       } 
       FormPage().fill_form(data).submit() 
       driver.quit() 

답변

0

은 실제로 두 개의 경고 및 오류가 있습니다

1 - "너무 광범위한 예외를"이것은 당신이 espefic 오류가 아닌 그들 모두 제외시켰다해야된다는 경고입니다. 귀하의 "제외"행에 except [TheExceptionYouAreTreating]:과 같은 것이 있어야합니다. 예제는 except ValueError:입니다. 그러나이 코드는 실행을 멈추지 않아야합니다.

2 - "오류 : 메서드가 어쩌면 정적 일 것입니다."이것은 submit 메서드가 정적 메서드임을 나타내는 경고입니다 (self 특성을 사용하지 않는 메서드 임) 이 실행 코드를 중지되는 것입니다 : "잘못된 구문 구문 에러"-이 경고를이 표시되지 않도록이

@staticmethod 
def submit(): 
    ... 

3처럼 장식 @staticmethod를 사용할 수 있습니다. 이는 코드에 무언가가 잘못 작성되었다는 오류입니다. 나는 그것이 당신 수업에 들여 쓰기일지도 모른다고 생각합니다. 사용해보기 :

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': '[email protected]', 
     } 
     FormPage().fill_form(data).submit() 
     driver.quit() 

한 가지 더. 그것들은 정말로 간단한 오류와 경고입니다. 여러분은 오류가 무엇을 말해야 하는지를주의 깊게 읽음으로써 스스로 고칠 수 있어야합니다. 나는 또한 당신을 읽는 것을 추천한다 Exceptions