Python(Selenium):如何使用登录重定向/组织登录登录网站 [英] Python (Selenium): how to login to a website with a login redirect/organization sign on

查看:91
本文介绍了Python(Selenium):如何使用登录重定向/组织登录登录网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是专业的程序员,所以请原谅任何愚蠢的错误——我正在做一些研究,我正在尝试使用 Selenium 登录到一个数据库来搜索大约 1000 个术语.

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.

我有两个问题:1.如何在重定向到页面上的组织标志后使用Selenium登录2. 如何搜索数据库

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

在我解决 1 之前,我无法真正得到 2,所以我真的只询问 1.

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()

当我导航到上述 URL 时,它会将我定向到我的组织登录页面(例如 login.universityname.edu),我应该能够在其中输入我的用户名和密码,然后它会将我定向到数据库,但是当我执行上面的代码时,它并没有让我登录.

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.

我可以在组织登录页面上找到的 html 如下所示:

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>

我认为可能有两个问题,但我不确定是哪个:1.要么我的代码试图在重定向之前输入登录信息,因此它没有输入任何内容;或者2. 我的 Selenium 代码没有正确识别组织登录的字段,所以它没有让我登录;或者3. 两者

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?

推荐答案

当您找到一个元素时,将 selenium 替换为 browser,这将起作用.

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.

注意:完成后不要忘记closequit浏览器.

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

这篇关于Python(Selenium):如何使用登录重定向/组织登录登录网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆