【问题标题】:How to click the Continue button using Selenium and Python如何使用 Selenium 和 Python 单击继续按钮
【发布时间】:2019-09-16 21:52:22
【问题描述】:

我正在尝试通过 Selenium 和 Python 3 从银行的在线服务中每月执行一些繁琐的复制/粘贴操作。不幸的是,我无法让 Selenium 单击登录链接。

这是https://www1.bmo.com/onlinebanking/cgi-bin/netbnx/NBmain?product=5 处的蓝色继续按钮。

奇怪的是,当我尝试在 Selenium 启动的浏览器中手动单击该链接时,它也不起作用 - 而它在我手动启动的浏览器中起作用。

我怀疑问题在于银行的网站足够智能,可以检测到我正在自动执行浏览器活动。有什么办法可以解决吗?

如果不是,会是别的吗?

我尝试过使用 Chrome 和 Firefox - 无济于事。我正在使用带有 Chrome 73.0.3683.103 和 Firefox 66.0 的 64 位 Windows 10 机器。

相关代码如下。

#websites and log in information
bmo_login_path = 'https://www1.bmo.com/onlinebanking/cgi-bin/netbnx/NBmain?product=5'
bmo_un = 'fake_user_name'
bmo_pw = 'fake_password'

#Selenium setup
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
chrome_driver_path = 'C:\\Path\\To\\Driver\\chromedriver.exe'
gecko_driver_path = 'C:\\Path\\To\\Driver\\geckodriver.exe'
browswer_bmo = webdriver.Firefox(executable_path = gecko_driver_path)
#browswer_bmo = webdriver.Chrome(executable_path = chrome_driver_path)

#log into BMO
browswer_bmo.get(bmo_login_path)
time.sleep(5)
browswer_bmo.find_element_by_id('siBankCard').send_keys(bmo_un)
browswer_bmo.find_element_by_id('regSignInPassword').send_keys(bmo_pw)
browswer_bmo.find_element_by_id('btnBankCardContinueNoCache1').click()

发送密钥非常有效。我实际上可能有错误的元素 ID(当我意识到我无法手动单击链接时,我试图在 Chrome 中测试它) - 但我认为更大的问题是我无法在启动的浏览器中手动单击链接通过硒。感谢您的任何想法。

编辑

这是我尝试单击继续按钮时得到的所有屏幕截图。

我在 IDE(Jupyter Notebook)中得到的错误信息最终是:

TimeoutException: Message: timeout
  (Session info: chrome=74.0.3729.108)
  (Driver info: chromedriver=74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729@{#29}),platform=Windows NT 10.0.17134 x86_64)

【问题讨论】:

  • 非常感谢。因为我正在旅行,所以我不得不搁置它,但是一旦我回到家,我就会重新努力并尝试 Selenium IDE 和 Kantu。

标签: python-3.x selenium-webdriver css-selectors webdriver webdriverwait


【解决方案1】:

要点击文本为 Continue 的按钮,您可以填写 Card NumberPassword 字段以诱导 WebDriverWait em> 对于element_to_be_clickable(),您可以使用以下解决方案:

  • 代码块:

    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
    options = webdriver.ChromeOptions()
    options.add_argument('start-maximized')
    options.add_argument('disable-infobars')
    options.add_argument('--disable-extensions')
    driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
    driver.get('https://www1.bmo.com/onlinebanking/cgi-bin/netbnx/NBmain?product=5')
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.dijitReset.dijitInputInner#siBankCard[name='FBC_Number']"))).send_keys("1234567890112233")
    driver.find_element_by_css_selector("input.dijitReset.dijitInputInner#regSignInPassword[name='FBC_Password']").send_keys("fake_password")
    driver.find_element_by_css_selector("span.dijitReset.dijitInline.dijitIcon.dijitNoIcon").click()
    # driver.quit()
    
  • 浏览器快照:

【讨论】:

  • 谢谢!不幸的是,当我尝试使用您的代码批发时,我仍然得到一个不可点击的按钮(以编程方式或手动方式)。可能是因为今天早上我在 Chrome 浏览器给我选项时不加思索地更新到了 74.0.3729.108?
  • @PKB 不知道为什么你会得到 unclickable button 但与 ChromeDriverChrome 的兼容版本这个代码块完美无缺。
  • 我也这么认为。就填充字段而言,我仍然能够让代码运行,但页面只是拒绝加载。 Selenium 给了我一个超时,但 Chrome 似乎只是无限期地旋转(我让它旋转了 20 多分钟而没有超时)。我承认我是一个初学者,所以我将尝试解析 HTML 并弄清楚你在用 CSS_SELECTOR 做什么,也许这会给我一些想法。再次感谢您。
  • 作为初学者,您能否从我的答案中复制粘贴代码,并使用兼容版本的 ChromeDriverChrome?
  • 谢谢,我又试了。我已经用我得到的截图编辑了我的帖子,使用了你的确切代码。我改变的只是更新的 Chromedriver 的路径。
【解决方案2】:

我能够通过在选项变量下方添加以下行来解决此问题并解决该问题。这会禁用 chrome 检查以实现自动化。我使用了整个销售代码,然后在启动驱动程序之前在正确的位置添加了以下行。

options.add_experimental_option("excludeSwitches", ['enable-automation'])

参考:https://help.applitools.com/hc/en-us/articles/360007189411--Chrome-is-being-controlled-by-automated-test-software-notification

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-01
    • 2021-04-09
    • 2021-04-08
    • 2014-01-04
    • 2021-01-08
    • 2018-08-22
    • 2023-02-01
    • 2019-09-01
    相关资源
    最近更新 更多