简体   繁体   中英

Python Selenium Web Automation How Do You Click A Login Button

I am trying to automate the downloads of specific files for my organization. In doing so I came across Browser Automation with Selenium. I got to the point where I can inject user credentials, but now I need to login to the page by clicking on the login button.

Here is the parent URL, in which I inject my credentials https://www.accuplacer.org

Then I need to click on the login button. Here is the inspect output of that element:

<button type="submit" class="btn btn-lg btn-primary pull-left " ng-disabled="loginDisable &amp;&amp; isFullyLoaded">
                                    <!-- ngIf: loginSpin && !traceIE -->
                                    <!-- ngIf: loginSpin && traceIE -->
                                    Login
                                 </button>

Here is the code I have so far, I know it's basic, and I am working on cleaning it up and defining somethings into functions.

import selenium
import os
import unittest
import requests
import time
from selenium import webdriver

#URL Variables
login_url = 'https://www.accuplacer.org/'
redirect_url = 'https://www.accuplacer.org/api/home.html#/'
reports_scheduler_url = 'https://www.accuplacer.org/api/home.html#/reportScheduler'
custom_reports_url = 'https://www.accuplacer.org/api/home.html#/customReports'

#WebDriver Path
browser = webdriver.Chrome("C:\Program Files (x86)\Google\Chrome\chromedriver.exe")

browser.get("https://www.accuplacer.org")

username = browser.find_element_by_id("login")
password = browser.find_element_by_id("password")
#submit = browser.find_element_by_id("Login")
username.send_keys("uname")
password.send_keys("test")

#browser.send_keys(Keys.ENTER)
#browser.send_keys(Keys.RETURN)

#submit.click()
browser.find_element_by_css_selector('btn btn-log btn-primary pull-left').click()

Your CSS locator is wrong. Try with below css locator with explicit wait.

element = WebDriverWait(browser, 20).until(
EC.element_to_be_clickable((By.CSS_SELECTOR, ".btn.btn-lg.btn-primary.pull-left")))
element.click();

For this piece of code you need below import

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

You can use the above, but I did not define the element in the way that Muzzamil did. Instead I just used the following for #submit.click

browser.find_element_by_css_selector('.btn.btn-lg.btn-primary').click()

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