2016-11-15 102 views
0

我想用Python在Selenium中自動化一個網頁文件下載程序。但是在使用Selenium單擊某個按鈕時遇到了一些困難:程序成功導致此URL'https://www.sec.gov/Archives/edgar/data/1467373/000119312510235847/0001193125-10-235847-index.htm',但它無法單擊第一個文檔(d10k.htm)的按鈕。該按鈕在以下代碼中定義爲'formbuttonElement',我通過Xpath對其進行跟蹤。另外,我使用了click()和.send_keys(Keys.SPACE)方法,但它們都不起作用。Python Selenium click()不起作用

有人可以看看這個問題嗎?

謝謝!

from selenium import webdriver 
from selenium.webdriver.support.ui import WebDriverWait 
import unittest 
import os 


class LoginTest(unittest.TestCase): 
def setUp(self): 
    fp=webdriver.FirefoxProfile(r"C:\Users\sxc\AppData\Roaming\Mozilla\Firefox\Profiles\x5i7u4m7.profileToolsQA") 

    fp.set_preference("browser.download.folderList",2) 
    fp.set_preference("browser.download.manager.showWhenStarting",False) 
    fp.set_preference("browser.download.dir", "D:\doc1") 
    fp.set_preference("pdfjs.disabled", True) 
    fp.set_preference("plugin.disable_full_page_plugin_for_types", "application/pdf") 

    fp.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/pdf") 


    self.driver=webdriver.Firefox(firefox_profile=fp) 
    self.driver.get("https://www.sec.gov/edgar/searchedgar/companysearch.html") 


def test_Login(self): 
    driver=self.driver 

    cikID="cik" 
    searchButtonID="cik_find" 
    typeID="//*[@id='type']" 
    priorID="prior_to" 
    cik="00001467373" 
    Type="10-K" 
    prior="201" 
    search2button="//*[@id='contentDiv']/div[2]/form/table/tbody/tr/td[6]/input[1]" 


    documentsbuttonid="documentsbutton" 
    formbuttonxpath="(//a[contains(@href,'/Archives/edgar/data/')])[1]" 


    cikElement=WebDriverWait(driver,30).until(lambda driver:driver.find_element_by_id(cikID)) 

    cikElement.clear() 
    cikElement.send_keys(cik) 


    searchButtonElement=WebDriverWait(driver,20).until(lambda driver:driver.find_element_by_id(searchButtonID)) 
    searchButtonElement.click() 

    typeElement=WebDriverWait(driver,30).until(lambda driver:driver.find_element_by_xpath(typeID)) 
    typeElement.clear() 
    typeElement.send_keys(Type) 
    priorElement=WebDriverWait(driver,30).until(lambda driver:driver.find_element_by_id(priorID)) 
    priorElement.clear() 
    priorElement.send_keys(prior) 
    search2Element=WebDriverWait(driver,30).until(lambda driver:driver.find_element_by_xpath(search2button)) 
    search2Element.send_keys(Keys.SPACE) 
    time.sleep(1) 

    documentsButtonElement=WebDriverWait(driver,20).until(lambda driver:driver.find_element_by_id(documentsbuttonid)) 
    documentsButtonElement.click() 

    formElement=WebDriverWait(driver,30).until(lambda driver:driver.find_element_by_xpath(formbuttonxpath)) 
    formElement.send_keys(Keys.SPACE) 



def terdown(self): 
    self.driver.quit() 
if __name__=='__main__': 
unittest.main() 

回答

1

試試這行代碼

driver.find_element_by_xpath('//a[text()="d10k.htm"]').click() 
+0

太好了!非常感謝你!! – SXC88

+0

嗨我有一個關於python的新問題,但以更一般的方式...也許你想看看! http://stackoverflow.com/questions/40744250/python-setting-limit-for-running-time-with-while-loop – SXC88