How to create an automatic login program on Mac using Python?
My motivation for creating this program is the inconvenience the login process brings to me whenever I get to this website, although I know you guys must be using the password saving feature, for my personal issue, I would not like to use this feature. Therefore, I generated the idea of making this little program.
I tried to search on the internet and I did find a website that teaches how to write such a program. But I did meet some other problems in this process.
First, we need to prepare the environment, and make sure you have 'pip' and 'selenium' installed in your environment.
1. Install pip
Open your terminal and type in the following codes one by one
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
and
python3 get-pip.py
Now, you should have pip installed on your computer, you can check the pip version by typing in the following code in terminal
pip3 --version
2. Install Selenium
Type in the following code to upgrade your pip
pip3 install --upgrade pip
Then, install Selenium
Type in the following code to install Selenium
pip3 install selenium
Done! Your initial work is done. Now we can get to the coding part.
3. The body of the program
Open your editor (I personally use Atom)
you can copy the following codes if you want
#import the webdriver module from selenium
from selenium import webdrive
#open the browser
driver = webdriver.Chrome(" ")
IMPORTANT! You need to input your own path of webdriver.[ In the (" ")]
For people who don't have webdriver on their computer, here are the links:
Chrome: sites.google.com/chromium.org/driver Edge: developer.microsoft.com/en-us/microsoft-edg.. Firefox: github.com/mozilla/geckodriver/releases Safari: webkit.org/blog/6900/webdriver-support-in-s..
After finishing this, Unzip your webdriver
Now you might be wondering where can you get the path of getting to your webdriver
Right-click on your webdriver, view profile,
See the 'where'? Copy it! Paste to your ' '!
4. Example display
Next, I'll use the Baidu search page(a search engine like google) as an example:
driver.get('https://www.baidu.com')
Now you are directed to the google page
Then, right-click the search block, click on inspect, then, the blue block you see you need to right-click it and choose copy, then, copy its XPath.
driver.find_element_by_xpath('//*[@id="kw"]').send_keys('this is a test')
In find_element_by_xpath(), paste your xpath into the parebtheses, and input content into .send_keys(' ')
Then it's the click button
driver.find_element_by_xpath('//*[@id="su"]').click()
The following are the whole codes:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://www.baidu.com')
driver.find_element_by_xpath('//*[@id="kw"]').send_keys('this is a test')
driver.find_element_by_xpath('//*[@id="su"]').click()
5. Apply
So now you get how the program is made, try to apply this method for your automatic login program!
In the automatic login program, prepare your username and password before alternate 'this is a test' in the example to your username and password, like this:
username = balabala
password = 123balala
driver.find_element_by_xpath('//*[@id="lUsername"]').send_keys('username')
driver.find_element_by_xpath('//*[@id="lPassword"]').send_keys('password')
To run this program, open your python launcher which is installed with your python3, and then on the top of the screen, click File, then open.
END
更多推荐
所有评论(0)