简体   繁体   English

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

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

I am trying to automate the log in process to WebAdvisor.我正在尝试将登录过程自动化到 WebAdvisor。 I have tried to select the "Log In" button by calling different elements.我试图通过调用不同的元素来 select 的“登录”按钮。 Each attempt so far has been unsuccessful.到目前为止,每一次尝试都没有成功。

My current code:我当前的代码:

path = '.../chromedriver

driver = webdriver.Chrome(path)

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

driver.get(url)

The below have been unsuccessful.下面的都失败了。

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

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

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

This is the section of the code related to the button I am trying to click on in the WebAdvisor website:这是与我尝试在 WebAdvisor 网站上单击的按钮相关的代码部分:

WebAdvisor的html代码

The expected result is the log in page.预期的结果是登录页面。 At the moment it does not change page.目前它没有改变页面。

Your xpath is wrong, Please find below solution.您的 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")

Induce WebDriverWait And element_to_be_clickable () And following locator startegy.诱导WebDriverWaitelement_to_be_clickable () 并跟随定位器启动。

Xpath : 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 Selector: 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()

You need to import following libraries.您需要导入以下库。

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

Browser Snapshot:浏览器快照:

在此处输入图像描述

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

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