python selenium启动常用

加载超时设置
browser.set_page_load_timeout(5)


# 二进制转16进制
a ='00000000'
hex(int(a, 2))
滑动元素
browser.execute_script("arguments[0].scrollIntoView(true);", i)

from selenium import webdriver
import time
from selenium.webdriver.common.action_chains import ActionChains
import re
from selenium.webdriver.common.keys import Keys
from bs4 import BeautifulSoup as bs
import pickle

options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches",["enable-logging"])
browser = webdriver.Chrome(options=options)
#browser = webdriver.Chrome()
#Chrome浏览器驱动
js='window.open("https://www.baidu.com");'
browser.execute_script(js)
browser.get('https://www.taobao.com/')
#打开网页
def dian(s,w):
    xx = browser.find_elements_by_css_selector(s)
    for i in xx:
        if w == i.text:
            print(i.text)
            i.click()
            time.sleep(1)
            break
        else:
            pass
handles = browser.window_handles
browser.switch_to.window(handles[0])

js="var q=document.getElementById('id').scrollTop=10000"
browser.execute_script(js)
js="var q=document.documentElement.scrollTop=100"
browser.execute_script(js)

js ='myVid=document.querySelector("video");alert(myVid.duration);'# 这里是获取第一个video标签
browser.execute_script(js)
#取得一个视频长度
js ='myVid=document.getElementById("videoSource");alert(myVid.duration);'
browser.execute_script(js)
try:
    alert = browser.switch_to.alert
    con = alert.text
    print(con)
    t = int(float(con)/60)+1
    print(t)
    alert.accept()
except:
    t =30

js = "var q=document.getElementsByClassName('content')[0].scrollTop = 10000"
#可指定元素滑动
def c():
    bo = browser.find_element_by_xpath('//*[@id="task-content-iframe"]')
    browser.switch_to.frame(bo)
    browser.switch_to.default_content()
    html = bo.get_attribute('innerHTML')
    js ='videoList(10108325)'
    browser.execute_script(ji)
    try:
        alert = browser.switch_to.alert
        con = alert.text
        print(con)
        alert.accept()
    except:
        pass

#元素截图
s = browser.find_element_by_id('safecode')
 s.screenshot('1.png')
#。。。。。。。。。。。。。。。。。。。。。。。。。。。

def c():
    #滑动元素
    for i in range(1):
        
        browser.switch_to.default_content()
        bo = browser.find_element_by_id('baxia-dialog-content')
        browser.switch_to.frame(bo)
        d = browser.find_element_by_id('nc_1_n1z')
        ActionChains(browser).click_and_hold(d).perform()
        
        xx= 300
        while xx>0:
            x = random.randint(50,80)
            xx =xx-x
            print(x)
            ActionChains(browser).move_by_offset(xoffset=x, yoffset=0).perform()

        ActionChains(browser).release(d).perform()

# selenium 4.0 
#id
x = browser.find_element(By.ID, 'kw') #id

#name
x = browser.find_element(By.NAME, 'wd') #id

#class
x = browser.find_element(By.CLASS_NAME, 'cos-pc') #id

#TAG p div span ul li
x = browser.find_element(By.TAG_NAME, 'body') #id

#By.LINK_TEXT表示全部匹配linktext定位找到元素
x = browser.find_element(By.LINK_TEXT, '百度首页') #id

#By.PARTIAL_LINK_TEXT表示模糊匹配linktext定位找到元素 
x = browser.find_element(By.PARTIAL_LINK_TEXT, '百度首页')

x = browser.execute_script(js)

def ma():

    def sss():
        import ddddocr
        ocr = ddddocr.DdddOcr()
        with open('1.png', 'rb') as f:
            img_bytes = f.read()
        res = ocr.classification(img_bytes)
        #print(res)
        return res

    for i in range(10):
        u = browser.find_element_by_id('checkcode')#验证码
        u.screenshot('1.png')
        m = sss()
        print(m)

        if len(m)==4:
            return m
        else:
            u.click()
            time.sleep(1)
            

def deng():

    for i in range(1):
        n = ''
        m =''

        browser.get('http://lzjtu.lllnet.cn/')
        time.sleep(5)
        nn =browser.find_element_by_id('loginName')
        mm = browser.find_element_by_id('password')
        num = browser.find_element_by_id('randNum')

        nn.clear()
        mm.clear()
        num.clear()

        a = ma()
        
        nn.send_keys(n)
        mm.send_keys(m)
        num.send_keys(a)

        time.sleep(1)
        browser.find_element_by_class_name('l_btn1').click()
        time.sleep(5)


def vo():#获取当前播放时间,如果两次时间没变就表示暂停了就点播放

    for i in range(1):

        try:
            js ='myVid=document.getElementById("videoMedia");alert(myVid.currentTime);'                     
            browser.execute_script(js)
            time.sleep(1)
        except:
            pass
        try:
            alert = browser.switch_to.alert
            con = alert.text
            t = float(con)
            alert.accept()
        except:
            t =0
        time.sleep(6)
        try:
            js ='myVid=document.getElementById("videoMedia");alert(myVid.currentTime);'                     
            browser.execute_script(js)
            time.sleep(1)
        except:
            pass
        try:
            alert = browser.switch_to.alert
            con = alert.text
            tt = float(con)
            alert.accept()
        except:
            tt =0
        if t==tt:
            try:
                browser.find_element_by_id('videoMedia').click()
            except:
                pass
        else:
            pass

chromedriver驱动下载地址:
最新下载链接:https
😕/googlechromelabs.github.io/chrome-for-testing/

http://chromedriver.storage.googleapis.com/index.html

国内镜像:https://registry.npmmirror.com/binary.html?path=chromedriver/

同时提供geckodriver和iedriverserver的下载地址:

geckodriver:

https://github.com/mozilla/geckodriver/releases

iedriverserver:

http://selenium-release.storage.googleapis.com/index.html
————————————————

def open_html(d):

for i in range(1):

    url =http://www.gongxukexuexi.com/

    headers ={}
    headers["Accept"]='*/*'
    headers["Accept-Encoding"]='gzip, deflate, br, zstd'
    headers["Accept-Language"]='zh-CN,zh;q=0.9'
    headers["Connection"]='keep-alive'
    headers["Content-Length"]='293'
    headers["Content-Type"]='application/x-www-form-urlencoded; charset=UTF-8'
    headers["Cookie"]='.sdlpalearnauth=D2BC9CF1F6C6507F915A3EA5E94F44C42A72042A387DFDFEC82C9F535C20467ECF46D00857E816DA9C6ADA1A66A7826D40F8B683DB7CE22A0EB5400419657AAD1A80791B2829F23D491F573826A7610D291ABC04E9FD4BA6F10631A3C03D0E0C492F3DB7EB605D7662BAE4E0F5E87F603F9A1163857901B063CE3A4FA7F2BE5244967516'
    headers["Host"]='sdutcm.sdlpa.org.cn'
    headers["Origin"]='https://sdutcm.sdlpa.org.cn'
    headers["Referer"]='https://sdutcm.sdlpa.org.cn/home/Exam/17?cId=512'
    headers["Sec-Fetch-Dest"]='empty'
    headers["Sec-Fetch-Mode"]='cors'
    headers["Sec-Fetch-Site"]='same-origin'
    headers["User-Agent"]='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36'
    headers["X-Requested-With"]='XMLHttpRequest'
    headers["sec-ch-ua"]='"Chromium";v="128", "Not;A=Brand";v="24", "Google Chrome";v="128"'
    headers["sec-ch-ua-mobile"]='?0'
    headers["sec-ch-ua-platform"]='"Windows"'

    data ='classId=17&endJson=%5B%7B%22id%22%3A21913%2C%22answer%22%3A%22A%22%7D%2C%7B%22id%22%3A21932%2C%22answer%22%3A%22A%22%7D%2C%7B%22id%22%3A21955%2C%22answer%22%3A%22A%22%7D%2C%7B%22id%22%3A21918%2C%22answer%22%3A%22A%22%7D%2C%7B%22id%22%3A21943%2C%22answer%22%3A%22A%22%7D%5D&cid=512&pid=18249788'
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值