python selenium启动常用

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


# 二进制转16进制
a ='00000000'
hex(int(a, 2))


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
————————————————

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值