繁体   English   中英

无法使用 Python 自动单击 Selenium 中的按钮

[英]Unable to automate the click of a button in Selenium with Python

我正在尝试将登录过程自动化到 WebAdvisor。 我试图通过调用不同的元素来 select 的“登录”按钮。 到目前为止,每一次尝试都没有成功。

我当前的代码:

path = '.../chromedriver

driver = webdriver.Chrome(path)

url = 'https://webadvisor.barry.edu/

driver.get(url)

下面的都失败了。

driver.find_element_by_id('acctLogin').click()

driver.find_element_by_name('Log In').click()

driver.find_element_by_link_text("Log In").click()

这是与我尝试在 WebAdvisor 网站上单击的按钮相关的代码部分:

WebAdvisor的html代码

预期的结果是登录页面。 目前它没有改变页面。

您的 xpath 错误,请查找以下解决方案。

from selenium import webdriver
from time import sleep
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By

driver = webdriver.Chrome(executable_path=r"chromedriver.exe")

driver.get("https://webadvisor.barry.edu/")


element=WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//li[@id='acctLogin']//span[@class='label'][contains(text(),'Log In')]")))
element.click()
element0=WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//input[@id='USER_NAME']")))
element0.send_keys("Test123")

诱导WebDriverWaitelement_to_be_clickable () 并跟随定位器启动。

Xpath

driver=webdriver.Chrome(path)
driver.get("https://webadvisor.barry.edu/")
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//li[@id='acctLogin']/a[./span[contains(.,'Log In')]]"))).click()

CSS 选择器:

driver=webdriver.Chrome()
driver.get("https://webadvisor.barry.edu/")
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"#acctLogin >a"))).click()

您需要导入以下库。

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

浏览器快照:

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM