Hướng dẫn python login script - kịch bản đăng nhập python

I'm looking for assistance to get my Python script to imitate a log-in feature while the credentials are stored in a separate file.

Nội dung chính Show

  • Table of contents
  • 1. Setting up Your Workspace
  • ChromeDriver
  • 3. How to hide our Passwords
  • 4. Reviewing Necessary Imports
  • 5. Brief Part on Web scraping
  • 6. Writing the Script
  • 7. Running The Program
  • How do I create a login script in Python?
  • How do I log into Python with a username and password?
  • How do I login to a website using Python?
  • How do I make a login and signup page in Python?

I got it to work from hard-coded Username and Password, and it also reads in a file, but I'm having some difficulty finding out how to link the two together.

Any assistance is appreciated.

The Python script is as follows:

print "Login Script"

import getpass

CorrectUsername = "Test"
CorrectPassword = "TestPW" 

loop = 'true'
while [loop == 'true']:

    username = raw_input["Please enter your username: "]

    if [username == CorrectUsername]:
        loop1 = 'true'
        while [loop1 == 'true']:
            password = getpass.getpass["Please enter your password: "]
            if [password == CorrectPassword]:
                print "Logged in successfully as " + username
                loop = 'false'
                loop1 = 'false'
            else:
                print "Password incorrect!"

    else:
        print "Username incorrect!"

I found this somewhere else that helped me read the file in, and it does print the contents of the text file, but I am unsure on how to progress from this:

with open['Usernames.txt', 'r'] as f:
    data = f.readlines[]
    #print data

for line in data:
    words = line.split[] 

The text file contains the Usernames and Passwords in a format of: Test:TestPW Chris:ChrisPW Admin:AdminPW with each credential on a new line.

As I said previously, any help is appreciated! Thanks.

In this article, we will be going over all the steps to prepare you to be able to open a webpage and log in using Python and some third-party applications. This is for developers at any level I will be going in-depth so everyone can fully understand this process.

Table of contents

  1. Setting up your workspace
  2. Downloading The necessary Tools
  3. How to hide our passwords
  4. Reviewing Imports
  5. Brief Part About Web Scraping
  6. Writing the Script
  7. Running The Program!

1. Setting up Your Workspace

First things first whenever we start working on a script, we want to make sure our set up is proper and organized. We will start by creating a folder I will be naming my folder auto_login. We will be holding 3 files total in this folder and it should look like this.

ChromeDriver

In this project, we will be making use of some 3rd party applications. The first one we will be using is called ChromeDriver You can download the program here.

Once on the page choose the appropriate link for your chrome version. Then you will choose the appropriate link according to your system. If you are on windows the 32bit link works regardless. You will then be downloading a zip file. Once downloaded we will move this zip file and extract it into the folder we have created. If you get stuck here, here is a video of how to download ChromeDriver for windows and for Mac.

Selenium

Selenium is a great program for working with automation and web applications, we will be using it for these exact purposes. The installation process for selenium is simple, you open the command prompt by searching up "cmd" on your computer. Open the command prompt and enter the following command:

pip install selenium

If you need more help with this installation or you run into issues, click here. Otherwise, let's move on.

3. How to hide our Passwords

If we must show our passwords while using this program we are not really able to show it off to our friends and where is the fun in that? In this article, I will show you a way to keep your login information hidden so you can share this with friends and not have worries about security. We will be creating another file and call it "login_details.py". We are going to keep this file as basic as possible inserting only 2 lines of code:

username = "username"
password = "password"

That is it, we will be importing this file into our code so when we want to show anyone our file the username and password will be hidden in our secret file.

4. Reviewing Necessary Imports

from selenium import webdriver
from selenium.webdriver.common.by import By
import login_details

This first part is where we import the modules required for the program. Beginning with Webdriver, it allows us to use our Python code to create test scripts. It is a web framework that allows for cross-browser testing. This tool will allow us to connect to a bunch of APIs and use them to test our web application.

The next line: from selenium.webdriver.common.by import By. This line allows us to locate elements By a specific name like the variables ID or class_name. This is important as the find_element[] is a key part of this program and understanding it is crucial.

The last line: import login_details. If you have not learned already, this is how our program is able to communicate to our secret passwords doc and get us that information. Without this, we have no username and password.

5. Brief Part on Web scraping

An important part of being able to use the web for automation is web scraping. This is the practice of using bots to grab the content of sites. We will need to locate certain IDs for this program and this is good knowledge to have about the internet and chrome. When we will be using the function find_element[By.id, "ID"] We need to locate that ID first because the function needs it, I will show you how to do this. Let's go to LinkedIn's sign-in page. I recommend going incognito for this, as it can make the process much easier. Whenever you want to find out about the properties of an object right-click on the object and then click inspect. So, hover over the "Email" box and inspect it. We can then see all properties, we are looking for the id which looks like, id="username". This is how you find the properties of certain objects. Once you inspect the email we are looking for a piece of code that looks like this, then we are copying id which is "username".


6. Viết kịch bản

Với thư mục tất cả được thiết lập và các tệp khác của chúng tôi trong cùng một thư mục với các tập lệnh Python của chúng tôi, chúng tôi có thể bắt đầu mã hóa! Hãy đặt tên cho tệp của chúng tôi "test_login.py" Chúng tôi sẽ bắt đầu bằng cách mang tên người dùng và mật khẩu của chúng tôi từ tệp login_details.py của chúng tôi. Nó sẽ trông giống thế này:

username = login_details.username
password = login_details.password

Chúng ta cần truy cập WebDriver và cụ thể cho Chrome mà chúng ta sẽ làm với dòng sau:

driver = webdriver.chrome[]

Dòng này chịu trách nhiệm tạo một thể hiện của WebDriver cho Chrome. Nếu bạn muốn sử dụng một trình duyệt khác, hãy trao đổi Chrome cho trình duyệt khác. Khi chúng tôi đã tạo một thể hiện, chúng tôi cần phải truy cập trang của chúng tôi. Chúng tôi sử dụng hàm trình điều khiển.get [] để làm như vậy. Hàm này sẽ đi đến trang chúng tôi nhập và chờ liên kết sau để tải đầy đủ. Đây là những gì nó trông giống như trong mã của chúng tôi, chúng tôi sẽ sử dụng LinkedIn cho ví dụ của chúng tôi. Mã tùy chọn là đặt trình duyệt lên toàn màn hình sau khi được tải được hiển thị trong trình điều khiển.get [].

driver.get["//www.linkedin.com/uas/login?"]
driver.maximize_window[]

Bây giờ tất cả những gì cần được thực hiện là nhập thông tin đăng nhập của chúng tôi và nhấn đăng nhập. Chúng tôi sẽ làm điều này với hàm find_element []. Cũng như hàm send.keys [], điều này được sử dụng để gửi thông tin của chúng tôi và thực sự nhập nó vào trình duyệt. Nó trông như thế này.

driver.find_element[By.ID, "login_field"].send_keys[username]
driver.find_element[By.ID, "password"].send_keys[password]
driver.find_element[By.XPATH,"//*[@id=\"organic-div\"]/form/div[3]/button"].click[]

Một khi bạn đã làm theo tất cả các bước này, chúng tôi rất tốt để đi.

7. Chạy chương trình

with open['Usernames.txt', 'r'] as f:
    data = f.readlines[]
    #print data

for line in data:
    words = line.split[] 
0

Trước khi chúng tôi bắt đầu, hãy đảm bảo thư mục được thiết lập chính xác vì vậy sau khi mọi thứ được thực hiện, thư mục sẽ giữ 3 tệp. Tập lệnh chính của chúng tôi được gọi là test_login.py, tệp tên người dùng và mật khẩu, được gọi là "login_details" và cuối cùng là ChromDriver.exe.

Hãy đảm bảo chương trình của chúng tôi hoạt động và bắt đầu sử dụng bot đầu tiên của chúng tôi. Trên đây là toàn bộ mã cần thiết để chạy chương trình của chúng tôi cho phép quay lại dấu nhắc lệnh và truy cập thư mục chúng tôi đã tạo. Sử dụng tập lệnh sau để chạy chương trình, trong đó file_name là tên của tệp của bạn.

with open['Usernames.txt', 'r'] as f:
    data = f.readlines[]
    #print data

for line in data:
    words = line.split[] 
1

Ở đó bạn có một bot tự đăng nhập với selenium, xin chúc mừng vì đã làm cho nó đến nay, may mắn!

Làm cách nào để tạo tập lệnh đăng nhập trong Python?

Tập lệnh python như sau: in "tập lệnh đăng nhập" nhập getpass ormentUsername = "test" orlyPassword = "testpw" loop = 'true' while [loop == 'true']: username = raw_input [" ] if [userName == orailUserName]: loop1 = 'true' while while [loop1 == 'true']: password = getPass.

Làm cách nào để đăng nhập vào Python bằng tên người dùng và mật khẩu?

Nhận tên người dùng và mật khẩu trong thời gian chạy bằng Python..

Pip Cài đặt GetPass. Python. Sao chép ..

Tên người dùng = getPass. GetUser [] Python. Sao chép ..

username = input ['nhập tên người dùng:'] python. Sao chép ..

Mật khẩu = GetPass. GetPass [] Python. Sao chép ..

Làm cách nào để đăng nhập vào một trang web bằng Python?

Từ WebBot Nhập trình duyệt Web = Browser [] Web. GO_TO ['Google.com'] Web. Nhấp vào ['Đăng nhập'] Web. Loại ['[Email & NBSP; được bảo vệ]', vào = 'Email'] Web ...

Kỳ thi này hoạt động tuyệt vời. ....

Không cài đặt trên win 64 bit. ....

Hãy thử sử dụng Python3. ....

Cách xử lý iframe trong webbot.? ...

Làm cách nào để tạo một trang đăng nhập và đăng ký trong Python?

Để làm điều đó chỉ cần sao chép mã bên dưới và dán nó vào bên trong trình chỉnh sửa văn bản nhàn rỗi ...

root = tk [].

nguồn gốc. Tiêu đề ["Python: Hệ thống kiểm kê đơn giản"].

chiều rộng = 640 ..

Chiều cao = 480 ..

màn hình_width = root. Winfo_ScreenWidth [].

Screen_Height = root. Winfo_Screenheight [].

x = [screen_width/2] - [chiều rộng/2].

y = [screen_height/2] - [chiều cao/2].

Bài Viết Liên Quan

Chủ Đề