国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

python腳本——selenium自動(dòng)化執(zhí)行一些網(wǎng)頁(yè)上的操作

這篇具有很好參考價(jià)值的文章主要介紹了python腳本——selenium自動(dòng)化執(zhí)行一些網(wǎng)頁(yè)上的操作。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

一、說(shuō)明

通過(guò)python的selenium模塊,自動(dòng)化執(zhí)行一些網(wǎng)頁(yè)上的重復(fù)的無(wú)聊的工作。

二、代碼

#! /usr/bin/python3.6
from time import sleep
import csv
import os
import tarfile
import shutil
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.wait import WebDriverWait

usr_name = "****"
usr_pwd = "****"
login_url = '****'
checkid = '*****'
# 左上角coverity cid,用于查詢頁(yè)面是否加載成功
cover_cid_xpath = '//*[@id="table-details-grid"]/div[1]/div[5]/div/div/div[1]/div'
# 分類歷史記錄xpath,用于查詢應(yīng)用是否成功
change_history_xpath = '//*[@id="source-browser-defect-history"]/div/div/div[2]'

# 選擇項(xiàng)目的xpath,搜索cid時(shí)的異常處理
exec_xpath = '//*[@id="defect-summaries"]/div[5]'
# chromedriver路徑
chrome_path = ""  # /media/vdb1/tools/pytoolpath/chromedriver

# 分析人和評(píng)審人
owner_name = ""
work_num = ''
warn_path = '/media/vdb1/workspace/告警'
download_path = '/media/vdb1/download/google'
owner_leader_name = ""
# 存儲(chǔ)Excel屬性的幾個(gè)list
err_index_list = []  # cid
err_url_list = []
err_intro_list = []


def move_untar_file():
    files = os.listdir(download_path)
    for file in files:
        if 'result' in file and '.gz' in file:
            src = os.path.join(download_path, file)
            dst = os.path.join(warn_path, file)
            print('src:', src)
            print('dst:', dst)
            shutil.move(src, dst)
            t = tarfile.open(dst)
            t.extractall(path=warn_path)
        elif '2022' in file and '.gz' in file:
            src = os.path.join(download_path, file)
            dst = os.path.join(warn_path, file)
            print('src:', src)
            print('dst:', dst)
            shutil.move(src, dst)
            t = tarfile.open(dst)
            t.extractall(path=warn_path)


# 從表格中導(dǎo)入數(shù)據(jù)
def import_excel(filepath):
    with open(filepath, newline='') as csvfile:
        reader = csv.reader(csvfile)
        for line in reader:
            err_index_list.append(line[0])
            err_url_list.append(line[1])
            err_intro_list.append(line[2])
    print('open file success!!!')


# 根據(jù)工號(hào)自動(dòng)定位告警文件并導(dǎo)入
def find_and_import_excel():
    global work_num
    dirs_hit = []
    files = os.listdir(warn_path)
    for file in files:
        if '2022' in file and '.gz' not in file:
            dirs_hit.append(warn_path + '/' + file)
    for dir in dirs_hit:
        files = os.listdir(dir)
        for file in files:
            owner_path = dir + '/' + file
            if '***_Low' in file and os.path.getsize(owner_path) != 0:
                import_excel(owner_path)


def auto_init():
    global chrome_path, owner_name, owner_leader_name, work_num
    chrome_path = '/media/vdb1/tools/pytoolpath/chromedriver'
    work_num = '****'
    owner_name = '****'
    owner_leader_name = '***'
    move_untar_file()
    find_and_import_excel()


def manual_init():
    global chrome_path, owner_name, owner_leader_name, work_num
    file_path = input('請(qǐng)輸入excel文件路徑:')
    if len(file_path) == 0:
        work_num = input('請(qǐng)輸入工號(hào):')
        if len(work_num) == 0:
            work_num = '****'
        find_and_import_excel()
    else:
        print('excel filepath: ' + file_path)
        import_excel(file_path)

    chrome_path = input('請(qǐng)輸入chromedriver文件路徑:')
    if len(chrome_path) == 0:
        chrome_path = '/media/vdb1/tools/pytoolpath/chromedriver'
    print('chrome filepath: ' + chrome_path)

    owner_name = input('請(qǐng)輸入分析人姓名:')
    if len(owner_name) == 0:
        owner_name = '****'
    print('分析人: ' + owner_name)

    owner_leader_name = input('請(qǐng)輸入評(píng)審人姓名:')
    if len(owner_leader_name) == 0:
        owner_leader_name = '****'
    print('評(píng)審人: ' + owner_leader_name)


# 環(huán)境初始化
def init_env():
    global chrome_path, owner_name, owner_leader_name, work_num
    init_mode = input('選擇模式:0、手動(dòng)輸入路徑  1、自動(dòng)處理 \n')
    if len(init_mode) == 0 or init_mode != '0':
        auto_init()
    else:
        manual_init()


# 頁(yè)面登錄
def chrome_login():
    chrome_options = webdriver.ChromeOptions()
    driver = webdriver.Chrome(executable_path=chrome_path, chrome_options=chrome_options)
    driver.get(login_url)  # 打開(kāi)頁(yè)面
    driver.find_element_by_name('username').send_keys(usr_name)
    driver.find_element_by_name('password').send_keys(usr_pwd)
    driver.find_element_by_id('login-button').click()  # 輸入用戶名和密碼,登錄

    # 判斷是否登錄成功
    WebDriverWait(driver, 20).until(lambda x: x.find_element_by_xpath(cover_cid_xpath).is_displayed())
    while 1:
        index_value = driver.find_elements_by_xpath(cover_cid_xpath)
        if len(index_value) > 0:
            if index_value[0].text == checkid:
                print('login success!!!')
                break
        sleep(1)

    # 打開(kāi)分類歷史記錄
    left_cid_xpath = '//*[@id="table-details-grid"]/div[1]/div[5]/div/div'
    driver.find_element_by_xpath(left_cid_xpath).click()
    history_button = '//*[@id="source-browser-defect-history"]/h3/span'
    WebDriverWait(driver, 20).until(lambda x: x.find_element_by_xpath(history_button).is_displayed())
    driver.find_element_by_xpath(history_button).click()
    return driver


def select_project(driver):
    print('項(xiàng)目選擇異常處理')
    WebDriverWait(driver, 2).until(lambda x: x.find_element_by_xpath(exec_xpath).is_displayed())
    search_res_lists = driver.find_elements_by_class_name('project-name')
    for search_res in search_res_lists:
        if 'ZXUBP' in search_res.text:
            print(search_res.text)
            search_res.click()
            break
    return driver


# 輸入cid并查詢
def err_id_search(driver, err_id):
    val_list = []
    time_count = 0
    driver.find_element_by_id('search').send_keys(err_id)
    driver.find_element_by_id('search-button').click()

    # 判斷是否加載新的cid成功
    WebDriverWait(driver, 20).until(lambda x: x.find_element_by_xpath(cover_cid_xpath).is_displayed())

    while 1:
        index_value = driver.find_elements_by_xpath(cover_cid_xpath)
        if len(index_value) > 0:
            for val in index_value:
                val_list.append(val.text)
            if str(err_id) in val_list:
                print(str(err_id) + ' load success!!!')
                break
        sleep(1)
        time_count += 1

        # 超時(shí)未打開(kāi),可能是需要選擇項(xiàng)目組
        if time_count > 5:
            try:
                # 判斷是否需要選擇項(xiàng)目組
                index_value = driver.find_elements_by_class_name('project-name')
                driver = select_project(driver)
                break
            except:
                index_value = driver.find_elements_by_xpath(cover_cid_xpath)
            time_count = 0
    return driver


# 告警處理
def err_handle(driver, err_id, err_intro):
    delay_count = 0
    selected_str = ''
    while 1:
        sleep(1)
        WebDriverWait(driver, 20).until(lambda x: x.find_element_by_name('classification').is_displayed())
        
        # index_value = driver.find_elements_by_xpath('//*[@id="source-browser-defect-history"]/div/div/div[2]/h4/span')
        # if len(index_value) > 0:
        #     print(index_value[0].text)

        try:
            selectTag = Select(driver.find_element_by_name('classification'))  # 分類下拉框
            selected_str = selectTag.first_selected_option.text
        except:
            print('get selected_str failed')

        if selected_str == '未分類' or delay_count >= 10:
            break
        delay_count += 1

    selectTag = Select(driver.find_element_by_name('classification'))
    # selectTag.select_by_value('22')  # 選擇誤報(bào)
    selectTag.select_by_visible_text('誤報(bào)')

    selectTag = Select(driver.find_element_by_name('severity'))
    selectTag.select_by_visible_text('未指定')  # 嚴(yán)重性選擇未指定

    selectTag = Select(driver.find_element_by_name('action'))  # 操作下拉框
    selectTag.select_by_visible_text('忽略')  # 選擇忽略

    # 填寫(xiě)處理意見(jiàn)
    err_comment = '【概述】: ' + err_intro
    err_comment = err_comment + '\n【技術(shù)分析】:無(wú)影響'
    err_comment = err_comment + '\n【技術(shù)影響】:無(wú)影響'
    err_comment = err_comment + '\n【分析人】:' + owner_name
    err_comment = err_comment + '\n【評(píng)審意見(jiàn)】:分析合理'
    err_comment = err_comment + '\n【評(píng)審人】:' + owner_leader_name
    driver.find_element_by_id("comment").clear()
    driver.find_element_by_id("comment").send_keys(err_comment)

    # 點(diǎn)擊應(yīng)用
    driver.find_element_by_id('source-browser-defect-triage-apply').click()
    # 查詢應(yīng)用是否成功
    WebDriverWait(driver, 20).until(lambda x: x.find_element_by_xpath(change_history_xpath).is_displayed())
    while 1:
        index_value = driver.find_elements_by_xpath(change_history_xpath)
        if len(index_value) > 0:
            if err_intro in index_value[0].text:
                print(str(err_id) + ' handle success!!!')
                break
        sleep(1)
    return driver


if __name__ == '__main__':
    init_env()
    start_index = 0
    if len(err_index_list) > 0:
        driver = chrome_login()
        for i in range(start_index, len(err_index_list)):
            driver = err_id_search(driver, err_index_list[i])
            driver = err_handle(driver, err_index_list[i], err_intro_list[i])
            print(str(i + 1) + '/' + str(len(err_index_list)) + ':' + err_index_list[i] + ' handle end')
        print('handle end!!')
    else:
        print('handle end!!')

三、用法總結(jié)

driver.find_element_by_name
driver.find_element_by_id
driver.find_element_by_xpath
WebDriverWait(driver, 20).until(lambda x: x.find_element_by_xpath(change_history_xpath).is_displayed())

chromdriver下載地址文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-545482.html

到了這里,關(guān)于python腳本——selenium自動(dòng)化執(zhí)行一些網(wǎng)頁(yè)上的操作的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 使用Python+selenium實(shí)現(xiàn)第一個(gè)自動(dòng)化測(cè)試腳本

    這篇文章主要介紹了使用Python+selenium實(shí)現(xiàn)第一個(gè)自動(dòng)化測(cè)試腳本,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧 最近在學(xué)web自動(dòng)化,記錄一下學(xué)習(xí)過(guò)程。 此處我選用python3.6+selenium3.0,均用

    2024年02月03日
    瀏覽(25)
  • Selenium+Python自動(dòng)化腳本環(huán)境搭建的全過(guò)程

    Selenium+Python自動(dòng)化腳本環(huán)境搭建的全過(guò)程

    * 本文僅介紹環(huán)境的搭建,不包含任何腳本編寫(xiě)教程。 先整體說(shuō)一下需要用到工具 1、Python環(huán)境(包括pip) 2、谷歌瀏覽器(包括對(duì)應(yīng)的WebDriver) 詳細(xì)步驟: 1、下載安裝包 Python Releases for Windows | Python.org ? 下載完成過(guò)后,打開(kāi)進(jìn)行安裝, 先把下面的add path打鉤 ,然后一直下

    2024年01月17日
    瀏覽(23)
  • Selenium+python怎么搭建自動(dòng)化測(cè)試框架、執(zhí)行自動(dòng)化測(cè)試用例、生成自動(dòng)化測(cè)試報(bào)告、發(fā)送測(cè)試報(bào)告郵件

    Selenium+python怎么搭建自動(dòng)化測(cè)試框架、執(zhí)行自動(dòng)化測(cè)試用例、生成自動(dòng)化測(cè)試報(bào)告、發(fā)送測(cè)試報(bào)告郵件

    本人在網(wǎng)上查找了很多做自動(dòng)化的教程和實(shí)例,偶然的一個(gè)機(jī)會(huì)接觸到了selenium,覺(jué)得非常好用。后來(lái)就在網(wǎng)上查閱各種selenium的教程,但是網(wǎng)上的東西真的是太多了,以至于很多東西參考完后無(wú)法系統(tǒng)的學(xué)習(xí)和應(yīng)用。 以下整理的只是書(shū)中自動(dòng)化項(xiàng)目的知識(shí)內(nèi)容,介紹怎么搭

    2024年02月05日
    瀏覽(30)
  • 【蘋(píng)果】SpringBoot監(jiān)聽(tīng)I(yíng)phone15郵件提醒,Selenium+Python自動(dòng)化搶購(gòu)腳本

    Iphone15來(lái)了,兩年之約你還記得嗎? 遂整合之前iphone13及iphone14的相關(guān)搶購(gòu)代碼,完成一個(gè) SpringBoot監(jiān)聽(tīng)I(yíng)phone15有貨郵件提醒+python自動(dòng)化腳本 小功能。 后端基于 SpringBoot ,通過(guò) 蘋(píng)果官網(wǎng)進(jìn)行有貨接口調(diào)用 ,將 JSON結(jié)果解析分析 是否有貨,并展示近10條有貨記錄列;可靈活監(jiān)聽(tīng)

    2024年02月08日
    瀏覽(52)
  • Python+Selenium.webdriver實(shí)現(xiàn)WEB端UI自動(dòng)化測(cè)試(實(shí)例腳本)

    Python+Selenium.webdriver實(shí)現(xiàn)WEB端UI自動(dòng)化測(cè)試(實(shí)例腳本)

    ????????本篇記錄基于Python+Selenium.webdriver實(shí)現(xiàn)WEB端UI自動(dòng)化測(cè)試,其中測(cè)試用例使用excel維護(hù)。為了在實(shí)際項(xiàng)目種的擴(kuò)展應(yīng)用,建議學(xué)習(xí)webdriver的元素定位方法,歡迎在評(píng)論區(qū)溝通討論。 該示例選取的是登錄頁(yè)不需要輸入驗(yàn)證碼校驗(yàn)的基礎(chǔ)頁(yè)面(考慮到現(xiàn)在大部分項(xiàng)目都是

    2023年04月08日
    瀏覽(34)
  • Postman-腳本自動(dòng)化及定時(shí)執(zhí)行腳本(7)

    Postman-腳本自動(dòng)化及定時(shí)執(zhí)行腳本(7)

    一.postman腳本自動(dòng)化 ( 從postman至Newman可以一鍵執(zhí)行腳本并生成報(bào)告: ) Postman Newman 是一個(gè) CLI(命令行界面)工具,可以使用它來(lái)運(yùn)行 Postman 中的集合(Collection)和環(huán)境(Environment)進(jìn)行自動(dòng)化測(cè)試。postman使用newman插件完成命令執(zhí)行postman腳本。?**(1)newman按裝** ?1.Newman安

    2024年01月21日
    瀏覽(25)
  • Selenium如何用于編寫(xiě)自動(dòng)化測(cè)試腳本?

    Selenium如何用于編寫(xiě)自動(dòng)化測(cè)試腳本?它提供了許多測(cè)試工具和API,可以與瀏覽器交互,模擬用戶操作,檢查網(wǎng)頁(yè)的各個(gè)方面。下面是一些步驟,可以幫助你編寫(xiě)Selenium自動(dòng)化測(cè)試腳本。 1、安裝Selenium庫(kù)和瀏覽器驅(qū)動(dòng)程序 首先,你需要安裝Selenium庫(kù)和瀏覽器驅(qū)動(dòng)程序,例如C

    2024年02月09日
    瀏覽(22)
  • Selenium自動(dòng)化腳本打包exe文件

    Selenium自動(dòng)化腳本打包exe文件

    近期由于工作需要寫(xiě)了一個(gè)selenium自動(dòng)化腳本,但是每次運(yùn)行的時(shí)候都要打開(kāi)Pycharm,因此本人直接使用Python第三方打包庫(kù)PyInstaller將py文件打包成一個(gè)可執(zhí)行的exe文件,在使用時(shí)無(wú)需安裝Python也可以點(diǎn)擊運(yùn)行。 PyInstaller是一個(gè)使用較為簡(jiǎn)單便捷的打包套件,只需要幾行命令即可

    2023年04月08日
    瀏覽(92)
  • selenium IDE自動(dòng)化測(cè)試腳本的實(shí)現(xiàn)

    selenium IDE自動(dòng)化測(cè)試腳本的實(shí)現(xiàn)

    Selenium IDE ? 是一個(gè)簡(jiǎn)單的錄制回放工具,它可以錄制你在瀏覽器上的操作,回放腳本時(shí),它可以重現(xiàn)錄制的動(dòng)作,就好像你又操作了一遍一樣。 ?selenium IDE是個(gè)瀏覽器插件,你需要在瀏覽器上安裝該插件才能使用。 1.下載chrome或者firefox瀏覽器的插件。 下載路徑:Selenium IDE

    2024年02月03日
    瀏覽(20)
  • 用selenium IDE手工編寫(xiě)自動(dòng)化測(cè)試腳本

    用selenium IDE手工編寫(xiě)自動(dòng)化測(cè)試腳本

    目錄 一、打開(kāi)selenium IDE插件 ?二、輸入腳本 1.元素定位的驗(yàn)證 ?console驗(yàn)證Xpath定位 2.元素定位的驗(yàn)證 ??console驗(yàn)證css定位 selenium IDE屬于玩具型自動(dòng)化測(cè)試腳本編寫(xiě)工具,它簡(jiǎn)單、易上手,對(duì)于web自動(dòng)化測(cè)試入門來(lái)說(shuō),是個(gè)非常不錯(cuò)的選擇。 使用seleniium IDE插件錄制腳本非常容

    2023年04月27日
    瀏覽(27)

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包