3

I am not a professional programmer, so please excuse any dumb mistakes--I am doing some research and I am trying to log into a database using Selenium to search it for about 1000 terms.

I have two issues: 1. How to log in using Selenium after a redirect to an organizational sign on page 2. How to search the database

Until I solve 1, I can't really get to 2, so I am really only asking about 1.

Here is the code I have so far:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

path_to_chromedriver = 'C:/Users/Kyle/Desktop/chromedriver' # change path as needed
browser = webdriver.Chrome(executable_path = path_to_chromedriver)
url = 'http://library.universityname.edu/corpaffils'
browser.get(url)

username = selenium.find_element_by_id("login_id")
password = selenium.find_element_by_id("login_password")

username.send_keys("my username")
password.send_keys("my password")

selenium.find_element_by_name("submit").click()

When I navigate to the above URL, it directs me to my organizational sign on page (say, login.universityname.edu), which I should be able to enter my username and password into, and then it would direct me to the database, but when I execute the code above, it does not log me in.

The html that I can find on the organizational sign in page looks like this:

<li><label for="login_id">ID:</label><input id="login_id" placeholder="ID" type="text" NAME="user" SIZE="20" VALUE=""/></li>
...
...
<li><label for="login_password">Password:</label><input id="login_password" placeholder="Password" type="password" NAME="pass" SIZE="20" /></li>
...
...
<ul class="submit">
<li><input type="SUBMIT" name="submit" value="Sign in"></li>
</ul>

I think there might be two issues, but I am not sure which it is: 1. Either my code is trying to enter the login information before the redirect, and thus it isn't entering into anything; or 2. My Selenium code is not properly identifying the fields for the organizational sign on, so it isn't logging me in; or 3. Both

Is there something I need to do to account for the redirect? Am I identifying the login fields correctly and handling them accurately?

MD. Khairul Basar
  • 4,976
  • 14
  • 41
  • 59
John Doe
  • 191
  • 1
  • 3
  • 13

1 Answers1

6

Replace selenium with browser when you are finding an element and this will work.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

path_to_chromedriver = 'C:/Users/Kyle/Desktop/chromedriver' # change path as needed
browser = webdriver.Chrome(executable_path = path_to_chromedriver)
url = 'http://library.universityname.edu/corpaffils'
browser.get(url)

username = browser.find_element_by_id("login_id")
password = browser.find_element_by_id("login_password")

username.send_keys("my username")
password.send_keys("my password")

browser.find_element_by_name("submit").click()

You don't need to do anything for redirecting. It will redirect automatically once it logs in.

N.B: Don't forget to close and quit the browser when you are done.

MD. Khairul Basar
  • 4,976
  • 14
  • 41
  • 59