【问题标题】:How to click on the button with text as Export as per the url through Selenium and Python?如何通过 Selenium 和 Python 根据 URL 单击带有文本的按钮作为导出?
【发布时间】:2018-09-04 09:41:02
【问题描述】:

我被困住了,我很想得到你的帮助。我不是 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 的电脑。

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

【问题讨论】:

  • Click 是一种方法,所以它应该是button.click()。你缺少括号。此外,如果您使用WebDriverWait 而不是.sleep() 会更好,例如button = WebDriverWait(driver, 20).until( EC.element_to_be_clickable((By.XPATH, text)));
  • 谢谢!!!! click() 是问题!!!
  • 没问题。我已将我的评论添加为答案,如果您想接受它,这样问题就不会无人回答。

标签: selenium selenium-webdriver webdriver webdriverwait


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    根据url,文本为Export的按钮是JavaScript启用元素,因此您需要诱导_WebDriverWait_以使元素可点击 您可以使用以下解决方案:

    • 代码块:

      from selenium import webdriver
      from selenium.webdriver.common.by import By
      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.support import expected_conditions as EC
      
      driver = webdriver.Firefox()
      driver.get("https://www.fec.gov/data/filings/?data_type=processed&committee_id=C00097485")
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.js-export.button.button--cta.button--export"))).click()
      
    • 浏览器快照:

    【讨论】:

      猜你喜欢
      • 2018-12-17
      • 2023-03-10
      • 2019-01-06
      • 1970-01-01
      • 2020-11-15
      • 2022-01-19
      • 2019-01-20
      • 2019-01-31
      • 2014-07-18
      相关资源
      最近更新 更多