Python Selenium click()不工作 [英] Python Selenium click() doesn't work
问题描述
我想在Python中使用Selenium自动化一个网络文件下载程序。但是我有点困难点击一个特定的按钮与Selenium:该程序成功导致这个网址' https://www.sec.gov/Archives/edgar/data/1467373/000119312510235847/0001193125-10-235847-index.htm ',但是它不能单击第一个文档的按钮(d10k.htm)。在以下代码中,按钮被定义为formbuttonElement,我通过Xpath跟踪它。此外,我使用了click()和.send_keys(Keys.SPACE)方法,但他们没有工作。
有人可以看看这个问题吗?
谢谢!
从selenium import webdriver
从selenium.webdriver.support.ui import WebDriverWait
import unittest
import os
class LoginTest(unittest.TestCase):
def setUp(self):
fp = webdriver.FirefoxProfile(rC:\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 [
documentbuttonid =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
$ b typeElement = WebDriverWait(driver,30).until(lambda driver:driver.find_element_by_xpath(typeID))
typeElement.clear()
typeElement.send_keys(Type)
(驱动程序,30).netDriverWait(驱动程序,30).until(lambda驱动程序:driver.find_element_by_id(priorID))
priorElement.clear()
priorElement.send_keys(先)
search2Element = WebDriverWait 30).until(lambda driver:driver.find_element_by_xpath(search2button))
search2Element.send_keys(Keys.SPACE)
time.sleep(1)
documentButtonElement = WebDriverWait ,20).until(lambda driver:driver.find_element_by_id(documentsbuttonid))
documentButtonElement.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 b $ b if __name __ =='__ main__':
unittest.main()
driver.find_element_by_xpath('// a [text =d10k.htm]')。click()
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()
Try this line of code
driver.find_element_by_xpath('//a[text()="d10k.htm"]').click()
这篇关于Python Selenium click()不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!