Python Selenium click() doesn't work

2019-09-12 02:42发布

问题:

I am trying to automate a web file downloading program with Selenium in Python. But I have some difficulties in clicking one particular button with Selenium: The program succeeds in leading to this url 'https://www.sec.gov/Archives/edgar/data/1467373/000119312510235847/0001193125-10-235847-index.htm', but it cannot click on the button of the first document (d10k.htm). The button is defined as 'formbuttonElement' in the following code and I tracked it by Xpath. In addition, I used both click() and .send_keys(Keys.SPACE) methods, but they didn't work.

Can someone have a look at this problem?

Thank you!

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="20101231"
    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:

Try this line of code

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