如何使用Python中的Selenium点击特殊按钮?

huangapple go评论60阅读模式
英文:

How to click special button with selenium in python?

问题

我正在尝试使用Selenium来点击美国银行模拟器内的某些按钮,但似乎这些按钮从未被点击。没有跳转到新链接,这是我以前没有遇到过的情况。

我想点击"Sign in options",然后点击"Sign in: Recognized device"。

我尝试使用Selenium点击按钮,没有出现错误。什么都没有发生,程序继续运行,所以我知道这不是找不到按钮的问题。我的当前代码如下:

driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.get('https://message.bankofamerica.com/onlinebanking_demo/OLB_Simulator/')

sleep(3)

login_button = driver.find_element("id", "landing_sign")
driver.execute_script("arguments[0].click();", login_button);

注意:以上是您提供的代码的翻译部分,不包括翻译请求。

英文:

I am trying to use selenium to click certain buttons within the bank of america simulator, but the buttons don't seem to ever click. No new link is reached, which is something I haven't encountered before.

https://message.bankofamerica.com/onlinebanking_demo/OLB_Simulator/

I want to click "Sign in options" and then click "Sign in: Recognized device"

I tried using selenium to click the button and I get no error. Nothing happens at all and the program continues, so I know it's not an issue with not finding the button. My current code is as follows:

driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.get('https://message.bankofamerica.com/onlinebanking_demo/OLB_Simulator/')

sleep(3)

login_button = driver.find_element("id", "landing_sign")
driver.execute_script("arguments[0].click();", login_button);

答案1

得分: 0

这段代码对我来说工作得很好。

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.get('https://message.bankofamerica.com/onlinebanking_demo/OLB_Simulator/')
wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.ID, "landing_sign")).click()
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[aria-labelledby='signInOpt3']")).click()

注意:使用sleep是不良实践,请使用WebDriverWait 并等待您需要的特定状态。

英文:

This code worked fine for me.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.get('https://message.bankofamerica.com/onlinebanking_demo/OLB_Simulator/')
wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.ID, "landing_sign")).click()
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[aria-labelledby='signInOpt3']")).click()

NOTE: Using sleep is a bad practice, use WebDriverWait and wait for the specific state you need instead.

答案2

得分: 0

要点击_可点击_元素,您需要使用WebDriverWait来等待element_to_be_clickable(),并且您可以使用以下定位策略:

  • 代码块:

    driver.get('https://message.bankofamerica.com/onlinebanking_demo/OLB_Simulator/')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#landing_sign"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[href='/onlinebanking_demo/OLB_Simulator/SignIn/recognized']")).click()
    
  • 注意: 您需要添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
  • 浏览器快照:

如何使用Python中的Selenium点击特殊按钮?

英文:

To click on the clickable elements you need to induce WebDriverWait for the element_to_be_clickable() and you can use the following locator strategies:

  • Code block:

    driver.get('https://message.bankofamerica.com/onlinebanking_demo/OLB_Simulator/')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#landing_sign"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[href='/onlinebanking_demo/OLB_Simulator/SignIn/recognized']"))).click()
    
  • Note: You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
  • Browser Snapshot:

如何使用Python中的Selenium点击特殊按钮?

huangapple
  • 本文由 发表于 2023年2月19日 11:14:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75497771.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定