首页 > 解决方案 > 如何通过 Selenium 和 Python 根据 URL 单击带有文本的按钮作为导出?

问题描述

我被困住了,我很想得到你的帮助。我不是 Python 天才,所以为这门语言道歉。我需要单击此网站https://www.fec.gov/data/filings/?data_type=processed&committee_id=C00097485上的按钮(导出) 。该按钮应驱动到出现 Excel 文件链接的页面底部。现在,我使用了这段代码:

text="//button[@type='button' and contains(.,'Export')]"
driver = webdriver.Firefox()
driver.get("https://www.fec.gov/data/filings/data_type=processed&committee_id=C00142711")
time.sleep(5)
button=driver.find_element_by_xpath(text)
button.click

脚本运行良好,没有错误消息。该网站出现,但没有发生“点击”。我也尝试过:1)“等待驱动程序直到元素可点击”,2)ActionChain 移动光标,3)用 sendKeys 替换点击。没有 iframe。我也在 Chrome 上试过。我正在使用装有 Windows 10 的电脑。

我究竟做错了什么???考虑到其他网站,点击功能非常好!

标签: seleniumselenium-webdriverwebdriverwebdriverwait

解决方案


Click 是一种方法,所以它应该是button.click()。你缺少括号。

此外,如果您使用WebDriverWait而不是会更好.sleep(),例如button = WebDriverWait(driver, 20).until( EC.element_to_be_clickable((By.XPATH, text)));


推荐阅读