python学习之自动登录网页

用到火狐的webdriver有点麻烦。
1.首先要去载geckodriver-v0.21.0-win64
地址:https://github.com/mozilla/geckodriver/releases
2.然后将gecodriver.exe放到和python.exe一个目录下
3.接着配置环境变量,将火狐安装目录C:\Program Files (x86)\Mozilla Firefox也配置到环境变量path中

from selenium import webdriver
import time
from urllib.request import urlopen
from bs4 import BeautifulSoup



def auto_login():
    username = "admin"
    password = "admin"




    url ="http://10.96.1.50:8089/spc/spc_logonPage.do"
    brower = webdriver.Firefox()
    brower.get(url)
    time.sleep(3)
    brower.find_element_by_name('username').send_keys(username)
    brower.find_element_by_id('password').send_keys(password)
    brower.find_element_by_name('Login').click()
    time.sleep(3)
    brower.find_element_by_xpath(".//*[@id='cssmenu']/ul/li[8]").click()  # 登录
    time.sleep(2)
    brower.close()
if __name__ == '__main__':
    for i in range(1,10):
        auto_login()

第二段为谷歌的

from selenium import webdriver
import time
def auto_login():
    username = "admin"
    password = "123"

    url = "http://10.104.160.185:8087/spc/spc_logonPage.do"

    brower = webdriver.Chrome()
    brower.maximize_window()
    brower.get(url)
    time.sleep(1)
    brower.find_element_by_name('username').send_keys(username)
    brower.find_element_by_id('password').send_keys(password)
    brower.find_element_by_name('Login').click()

    time.sleep(1)
    brower.find_element_by_xpath(".//*[@id='cssmenu']/ul/li[3]").click()
    brower.find_element_by_xpath(".//*[@id='cssmenu']/ul/li[3]/ul/li[1]").click()
    brower.find_element_by_name('saveConfigForm').click()
    n = brower.window_handles
    brower.switch_to.window(n[1]) #切换句柄
    brower.find_element_by_name("selectedConfigList").click()
    brower.switch_to.window(n[0])
    brower.find_element_by_name("goButton").click()
    time.sleep(100)
    brower.quit()

if __name__ == '__main__':
        auto_login()

你可能感兴趣的:(python学习之自动登录网页)