zoukankan      html  css  js  c++  java
  • selenium浅谈,Chrome与无头浏览器

    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support.select import Select
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.by import By
    from selenium.webdriver.common.action_chains import ActionChains
    from selenium.webdriver.common.keys import Keys
    import time
    from selenium.webdriver.chrome.options import Options


    # 选择哪一个驱动,return driver句柄
    def web1(web=4):
    if web == 4: # 使用nohedld-chrome
    chrome_options = Options()
    # 加属性,使用无头模式
    chrome_options.add_argument('--headless')
    # 加属性,规避chrome自身bug
    chrome_options.add_argument('--disable-gpu')
    # 找到driver
    driver = webdriver.Chrome(options=chrome_options)
    # 浏览器最大化
    driver.maximize_window()
    driver.get('url')
    # return driver句柄
    return driver
    else:
    driver = webdriver.Chrome()
    driver.maximize_window()
    driver.get('url')
    return driver


    # time 操作时间模块
    def web_time(second):

    return time.sleep(second)


    # 截图
    def web_png():
    driver = web1()
    screen = driver.get_screenshot_as_file('文件路径+图片名称')
    screen2 = driver.save_screenshot('文件路径+图片名称')
    return screen, screen2


    #获取属性值
    def web_attribute():
    driver = web1()
    attribute = driver.find_element_by_id('xxx').get_attribute('class')
    return attribute


    # js调用
    def web_js():
    driver = web1()
    # js 点击
    driver.execute_script('arguments[0].click()','elment')
    # js 输入
    driver.execute_script('arguments[0].value=“xxx”', 'elment')
    # js 鼠标滑动
    driver.execute_script('arguments[0].ScrollIntoview()', 'elment')


    # select
    def web_select():
    driver = web1()
    ele = driver.find_element_by_id('xxx')
    # 通过Select选项的索引
    Select(ele).select_by_index('1')
    # 通过Select选项的值
    Select(ele).select_by_value('20')
    # 通过Select的选项的value属性值来定位
    Select(ele).select_by_visible_text('每页显示20条')


    # Actionchains鼠标操作
    def web_actions():
    driver = web1()
    move = driver.find_element_by_id('xxx')
    # 鼠标移动到move元素
    ActionChains(driver).move_to_element(move).perform()
    # 鼠标移动到(x,y)坐标
    ActionChains(driver).move_by_offset(move).perform()
    # 鼠标拖拽到move1元素
    move1 = driver.find_element_by_id('xxx')
    ActionChains(driver).drag_and_drop(move).perform()
    # 鼠标拖拽到某个(x,y)坐标
    ActionChains(driver).drag_and_drop_by_offset('x','y').perform()


    # Keys键盘操作
    def web_keys():
    driver = web1()
    # 快捷键全选
    driver.find_element_by_id('xx').send_keys(Keys.CONTROL+'a')
    # 快捷键剪切
    driver.find_element_by_id('xx').send_keys(Keys.CONTROL,'x')
    # 快捷键粘贴
    driver.find_element_by_id('xx').send_keys(Keys.CONTROL+'v')
    # 快捷键复制
    driver.find_element_by_id('xx').send_keys(Keys.CONTROL+'c')
    # 快捷键删除
    driver.find_element_by_id('xx').send_keys(Keys.BACK_SPACE)
    # 快捷键空格
    driver.find_element_by_id('xx').send_keys(Keys.SPACE)
    # 快捷键回车
    driver.find_element_by_id('xx').send_keys(Keys.ENTER)


    # 隐形等待,页面每个元素都进行等待,增加执行时间
    def web_imp(second):
    driver = web1()
    return driver.implicitly_wait(second)


    # 显性等待,配合until()跟until_not()方法,根据EC灵活等待
    def web_wait():
    driver = web1()
    # 程序每0.2秒看一次页面,如果成立就执行下一步,否则继续等待,超过5秒抛出TimeoutException
    WebDriverWait(driver, 5, 0.2).until(EC.visibility_of_element_located(By.XPATH, 'ele')).click()


    # EC模块作为预期结果,经常与until()跟until_not()连用
    def web_ec():
    # 验证元素是否出现,一个符合元素就行
    EC.presence_of_element_located(By.ID, 'ele')
    # 验证元素是否出现,需要等所有符合元素都加载出来
    EC.presence_of_all_elements_located(By.ID, 'ele')
    # 验证元素是否可见,传入参数是元组locator
    EC.visibility_of_element_located(By.ID, 'ele')
    # 验证元素是否可见,传入参数是元组locator
    EC.invisibility_of_element_located(By.ID, 'ele')
    # 验证元素是否可见,传入参数是webelement,跟visibility_of_element_located(By.ID, 'ele')效果一样
    EC.invisibility_of_element_located('ele')
    # 判断元素是否可以点击,传入元组locator
    EC.element_to_be_clickable(By.ID, 'ele')
  • 相关阅读:
    防抖函数
    video.js汉化
    vscode 设置
    webpack配置
    寄生组合继承
    数组排序
    操作节点的方法
    vscde软件
    vue目录详解
    前端
  • 原文地址:https://www.cnblogs.com/12260420zxx/p/13657633.html
Copyright © 2011-2022 走看看