python selenium浏览器模拟
程序员文章站
2024-02-25 22:05:15
...
python selenium浏览器模拟
1、Download the browser’s driver from selenium official webside,and driver should be compatible with your system and your browser.
2、Copy the driver to browser’s and python’s execution route.
3、Add browser’s execution route to system path .
import time
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
import sqlite3 as db
import random
#get username and password from database
conn=db.connect("./portal/user.sqlite")
cursor=conn.cursor()
conn.row_factory=db.Row
cmd="SELECT username,password FROM 'portal_user'"
cursor.execute(cmd)
rows=cursor.fetchall()
url="http://***"
NUM=10
#setting the browser windows invisiable
options=Options()
options.headless=True
selected_rows=random.choices(rows,k=NUM)
for i in range(NUM):
try:
usern=selected_rows[i][0]
psw=selected_rows[i][1]
browser=webdriver.Firefox(options=options)
browser.get(url)
time.sleep(3)
#searching html element by id and then sending content
browser.find_element_by_id("username").send_keys(usern)
browser.find_element_by_id("password").send_keys(psw)
#button action method
browser.find_element_by_id("submit_login").click()
time.sleep(1)
#jump to new webside by clicking
browser.find_element_by_xpath("//a[@href='http://**]").click()
browser.close()
print(selected_rows[i][0],"successfully login!")
time.sleep(2)
except:
print(selected_rows[i][0],"failed to login!")
上一篇: C语言羁绊之数组长度的求解方式
下一篇: 字符串长度
推荐阅读