Python 使用脚本登录网站

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34393353/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 14:56:04  来源:igfitidea点击:

Login to a website using script

python

提问by v1shnu

I am actually writing a windows script which on clicking will open my default web browser , open a page , enter my login details and then submit the form.

我实际上正在编写一个 Windows 脚本,单击该脚本将打开我的默认 Web 浏览器,打开一个页面,输入我的登录详细信息,然后提交表单。

All I could do so far was open the browser with this:

到目前为止我能做的就是用这个打开浏览器:

explorer http:\outlook.com\website.com

Since I could not find any resource which tells how to do this, I looked at python scripting and found two libs ie. urlliband webbrowser. I am know that urllibis used for scraping websites. But I am not sure how to simulate the same in a web browser. The webbrowserlibrary has only commands to open the page in a new tab etc.

由于我找不到任何说明如何执行此操作的资源,因此我查看了 python 脚本并找到了两个库,即。urllibwebbrowser。我知道这urllib是用于抓取网站。但我不确定如何在网络浏览器中模拟相同的内容。该webbrowser库只有在新选项卡等中打开页面的命令。

Please tell me how I can achieve this task either using python or windows script.

请告诉我如何使用 python 或 windows 脚本完成此任务。

EDIT:I have my username and password saved in my browser so that it auto fills. I just want to simulate the 'ENTER' key press.

编辑:我的用户名和密码保存在我的浏览器中,以便它自动填充。我只想模拟 'ENTER' 按键。

回答by Jamie Bull

IEC

国际电工委员会

For me the easiest to use option for controlling a browser from Python is IEC. I find very easy to use for simple things like you describe.

对我来说,从 Python 控制浏览器最容易使用的选项是IEC。我发现像您描述的那样简单的事情很容易使用。

IEC is a python library designed to help you automate and control an Internet Explorer window. You can use this library to navigate to web pages, read the values of various HTML elements, set the values of checkboxes, text boxes, radio buttons etc., click on buttons and submit forms.

Typical uses for this library include:

  • Writing test scripts to check your web application automatically.

  • Managing an online account automatically.

  • Automatically logging and downloading webmail.

  • Monitoring web based applications periodically for failure.

IEC 是一个 Python 库,旨在帮助您自动化和控制 Internet Explorer 窗口。您可以使用该库导航到网页,读取各种 HTML 元素的值,设置复选框、文本框、单选按钮等的值,单击按钮并提交表单。

该库的典型用途包括:

  • 编写测试脚本以自动检查您的 Web 应用程序。

  • 自动管理在线帐户。

  • 自动记录和下载网络邮件。

  • 定期监控基于 Web 的应用程序是否出现故障。

Selenium

If you'd rather avoid Internet Exporer (and who wouldn't?) then you can use selenium. To install it do:

如果您宁愿避免使用 Internet Exporer(谁不想?),那么您可以使用selenium. 要安装它,请执行以下操作:

$ pip install -U selenium

There are details for how to login to a site with seleniumusing Chrome on this question. The important part is:

selenium这个问题上有关于如何使用 Chrome登录网站的详细信息。重要的部分是:

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

chromedriver = 'C:\chromedriver.exe'
browser = webdriver.Chrome(chromedriver)
browser.get('http:\outlook.com\website.com')

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

username.send_keys("YourUsername")
password.send_keys("Pa55worD")

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

回答by Pierre Michard

seleniumis a tool designed to test websites, so it is able to manage a large variety of browsers.

selenium是一种设计用于测试网站的工具,因此它能够管理多种浏览器。

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

browser = webdriver.Firefox()
browser.get("http:\outlook.com\website.com") 
time.sleep(10) 
username = browser.find_element_by_id("user_input_id") 
password = browser.find_element_by_id("password_input_id")
username.send_keys("your_username") 
password.send_keys("your_password") 
login_attempt = browser.find_element_by_xpath("//*[@type='submit']")
login_attempt.submit()

To retrieve your default browser name you can use webbrowserand create a mapping between webbrowser class type and selenium browser driver.

要检索默认浏览器名称,您可以使用webbrowser并在 webbrowser 类类型和 selenium 浏览器驱动程序之间创建映射。

browser = webbrowser.get()
print browser.__class__

回答by grepit

All the previous answers are correct here is an example for Chrome and Facebook. You can use the same code but need to change the elements it's looking for and of course you have to give it, correct url, user name and password

以前的所有答案都是正确的,这里是 Chrome 和 Facebook 的示例。您可以使用相同的代码,但需要更改它要查找的元素,当然您必须提供正确的 url、用户名和密码

Note: this was test on Macbook

注意:这是在 Macbook 上测试的

  1. download the selenium driver by:

       cd /tmp
       wget https://chromedriver.storage.googleapis.com/2.41/chromedriver_mac64.zip 
       unzip chromedriver_mac64.zip -d /usr/local/bin
    
  2. The code that does the work is below and again make sure you change the url, elements it looks for and user name and password. ( assumptions, open a file name called login.py and copy and paste the below code in it and then make the changes)

    from selenium import webdriver
    
    from selenium.webdriver.common.keys import Keys
    
    from time import sleep
    
    driver = webdriver.Chrome()
    driver.get("https://en-gb.facebook.com/login/")
    elem = driver.find_element_by_name("email")
    elem.clear()
    elem.send_keys("YOUR_USER_NAME")
    
    pas = driver.find_element_by_name("pass")
    pas.clear()
    pas.send_keys("YOUR_PASSWORD")
    pas.send_keys(Keys.RETURN)
    
    sleep(100)
    driver.close()
    
  3. run it:

    python login.py

  1. 通过以下方式下载硒驱动程序:

       cd /tmp
       wget https://chromedriver.storage.googleapis.com/2.41/chromedriver_mac64.zip 
       unzip chromedriver_mac64.zip -d /usr/local/bin
    
  2. 完成这项工作的代码如下,再次确保您更改了 url、它查找的元素以及用户名和密码。(假设,打开一个名为 login.py 的文件并将以下代码复制并粘贴到其中,然后进行更改)

    from selenium import webdriver
    
    from selenium.webdriver.common.keys import Keys
    
    from time import sleep
    
    driver = webdriver.Chrome()
    driver.get("https://en-gb.facebook.com/login/")
    elem = driver.find_element_by_name("email")
    elem.clear()
    elem.send_keys("YOUR_USER_NAME")
    
    pas = driver.find_element_by_name("pass")
    pas.clear()
    pas.send_keys("YOUR_PASSWORD")
    pas.send_keys(Keys.RETURN)
    
    sleep(100)
    driver.close()
    
  3. 运行:

    蟒蛇登录.py

enter image description here

在此处输入图片说明

回答by programmerX

u'\ue007'is an enter code in selenium.webdriver.common.keys library. You might try to press "enter" after you sent your username and password.

u'\ue007'是 selenium.webdriver.common.keys 库中的输入代码。您可以在发送用户名和密码后尝试按“回车”。

username = browser.find_element_by_id("user_input_id")   
username.send_keys("myUserName") username.send_keys(u'\ue007') 
password = browser.find_element_by_id("password_input_id")  
password.send_keys("myPassword")  password.send_keys(u'\ue007')