Selenium常用:
Python-Selenium基本操作
一、html
下面的html是后面代码中使用到的index.html文件,页面大致如下:
<!DOCTYPE html>
<html>
<head>
<title>鼠标键盘</title>
<meta charset="utf-8">
<style type="text/css">
#acontainer{
background: red;
}
</style>
</head>
<body>
<div id="container">
<form>
user:<input type="text" name="user" id="user"><br /><br />
email:<input type="text" name="email" id="email"><br /><br />
<br /><br />
<button id="login">login</button>
</form>
</div>
<div id="acontainer">
<a href="abc" class="aele">abc</a><br />
<a href="bbc" class="aele">bbc</a><br />
<a href="def" class="aele">def</a><br />
</div>
</body>
</html>
<script type="text/javascript">
let login = document.getElementById("login")
login.onclick = function() {
alert("login")
}
document.onkeydown = function(event){
if(event.keyCode ==13){
alert("login")
}
}
let acontainer = document.getElementById("acontainer")
acontainer.addEventListener('dblclick',function(){
let aele = document.createElement("a");
let br = document.createElement("br");
let num = Math.random();
aele.href= num;
aele.innerText = num;
acontainer.appendChild(aele)
acontainer.appendChild(br)
},false);
</script>
二、cookie
方法属性 | 说明 |
---|---|
driver.get_cookies | 获得cookie信息 |
add_cookie | 添加会话信息 |
delete_cookie | 删除指定cookie |
delete_all_cookies | 删除所有cookie |
from selenium import webdriver
options = webdriver.FirefoxOptions()
options.headless = True
driver = webdriver.Firefox(options=options)
def printCks(cookies):
print(type(cookies))
for ck in cookies:
print(type(ck), end=": ")
print(ck)
print("-------------")
driver.get(r'https://www.baidu.com/index.php?tn=monline_3_dg')
# 将获得cookie的信息
cks = driver.get_cookies()
printCks(cks)
driver.add_cookie({'name': 'cname', 'value': 'cvalue'})
cks = driver.get_cookies()
printCks(cks)
# 删除指定name的cookie
driver.delete_cookie("cname")
cks = driver.get_cookies()
printCks(cks)
# 删除所有cookie
driver.delete_all_cookies()
cks = driver.get_cookies()
printCks(cks)
三、键盘操作
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
TIME_SLEEP = 3
# options = webdriver.FirefoxOptions()
options = webdriver.ChromeOptions()
options.headless = False
# driver = webdriver.Firefox(options=options)
driver = webdriver.Chrome(options=options)
file_path = r'file:///F:\tmp\index.html'
driver.get(file_path)
time.sleep(TIME_SLEEP)
driver.find_element_by_id("user").send_keys("curitis@gmail.com")
time.sleep(TIME_SLEEP)
# ctrl + a
driver.find_element_by_id("user").send_keys(Keys.CONTROL, 'a')
# ctrl + c
driver.find_element_by_id("user").send_keys(Keys.CONTROL, 'c')
# ctrl + v
driver.find_element_by_id("email").send_keys(Keys.CONTROL, 'v')
time.sleep(TIME_SLEEP)
# 回车
driver.find_element_by_id("login").send_keys(Keys.ENTER)
time.sleep(TIME_SLEEP)
driver.switch_to.alert.accept()
四、鼠标操作
方法属性 | 说明 |
---|---|
context_click | 右击 |
double_click | 双击 |
drag_and_drop | 拖动 |
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time
TIME_SLEEP = 3
# options = webdriver.FirefoxOptions()
options = webdriver.ChromeOptions()
options.headless = False
# driver = webdriver.Firefox(options=options)
driver = webdriver.Chrome(options=options)
file_path = r'file:///F:\tmp\index.html'
driver.get(file_path)
time.sleep(TIME_SLEEP)
acontainer = driver.find_element_by_id("acontainer")
# 双击acontainer
ActionChains(driver).double_click(acontainer).perform()
time.sleep(TIME_SLEEP)
ActionChains(driver).double_click(acontainer).perform()
time.sleep(TIME_SLEEP)
ActionChains(driver).double_click(acontainer).perform()
time.sleep(TIME_SLEEP)
driver.quit()