简体   繁体   中英

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

I am trying to automate the log in process to WebAdvisor. I have tried to select the "Log In" button by calling different elements. 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的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.

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.

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:

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:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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