Python-Selenium操作鼠标键盘Cookie

Selenium常用:
Python-Selenium基本操作

Python-Selemium元素定位

Python-Selenium操作鼠标键盘Cookie

一、html

下面的html是后面代码中使用到的index.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()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值