Python--selenium

seleniu 一点常用的操作

import random
import time
from datetime import datetime

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.remote.webdriver import WebElement


def easyui_date_select():
    # easyui日期控件操作
    browser = webdriver.Chrome()
    url = ''
    browser.get(url)
    # 日期选择小图标
    try:
        combo_arrow = browser.find_element_by_xpath(
            '//span[contains(@class, "combo-arrow")]'
        )
    except Exception as e:
        print('没有找到日期控件')
        return False
    combo_arrow.click()
    today = datetime.today()
    target_date = datetime.strptime('2021-11-11', '%Y-%m-%d')  #
    gap_y = today.year - target_date.year
    gap_m = today.month - target_date.month
    if gap_y != 0:
        # 切换年
        y_tag = 'calendar-prevyear' if gap_y > 0 else 'calendar-nextyear'
        gap_y = abs(gap_y)
        while gap_y > 0:
            y_btn = WebDriverWait(browser, 10).until(
                EC.presence_of_element_located(
                    (By.XPATH, f'//div[contains(@class,"{y_tag}")]')
                )
            )
            if y_btn is None:
                print('没有找到年份前进或后退按钮')
                return False
            y_btn.click()
            gap_y -= 1
            time.sleep(0.5)
    if gap_m != 0:
        # 切换月
        m_tag = 'calendar-prevmonth' if gap_m > 0 else 'calendar-nextmonth'
        gap_m = abs(gap_m)
        while gap_m > 0:
            y_btn = WebDriverWait(browser, 10).until(
                EC.presence_of_element_located(
                    (By.XPATH, f'//div[contains(@class,"{m_tag}")]')
                )
            )
            if y_btn is None:
                print('没有找到月份前进或后退按钮')
                return False
            y_btn.click()
            gap_m -= 1
            time.sleep(0.5)
    # 选择目标日期并点击
    date_element = WebDriverWait(browser, 10).until(
        EC.presence_of_element_located(
            (By.XPATH, f'//td[@abbr="{target_date.year},{target_date.month},{target_date.day}"]',)
        )
    )
    target_date_str = target_date.strftime('%Y-%m-%d')
    if date_element is None:
        print(f'没有找到日期{target_date_str}')
        return False
    date_element.click()
    time.sleep(10)
    browser.quit()
    return True


def key_operator():
    browser = webdriver.Chrome()
    url = ''
    browser.get(url)
    element = WebDriverWait(browser, 10).until(
        EC.presence_of_element_located(
            (By.XPATH, '//element_xpath',)
        )
    )
    element_list = WebDriverWait(browser, 10).until(
        EC.presence_of_all_elements_located(
            (By.XPATH, '//element_xpath',)
        )
    )
    element = WebElement(element)  # 可以不用,这里主要是方便后面写代码自动提示 :·)
    element.clear()
    element.send_keys('xxx')
    element.click()  # 点击
    ActionChains(browser).move_to_element(elememt).click().perform()  # 点击
    browser.execute_script('arguments[0].click()', element)  # 点击
    element.is_displayed()  # 判断是否可见
    element.screenshot(f'./img/{time.time()}.png')  # 保存到图片文件
    element_png = element.screenshot_as_png  # 图片二进制数据
    element_base64 = element.screenshot_as_base64  # 图片二进制数据Base64编码后数据
    element_text = element.text  # 文本信息
    element_attr_value = element.get_attribute('value')
    element.find_element_by_xpath('xpath')
    element.find_element(By.XPATH, 'xpath')
    element.find_elements_by_xpath('xpath')
    element.find_elements(By.XPATH, 'xpath')
    element_size = element.size
    element_location = element.location
    # 执行js脚本
    browser.execute_script('arguments[0].value="xxx"', element)
    browser.execute_script('javascript')
    # 使用Action 定位element并滑动element
    action = webdriver.ActionChains(browser)
    random_x = random.choice(range(0, element_size.get('width', 1)))
    random_y = random.choice(range(0, element_size.get('height', 1)))
    print('element: ', element_size, element_location, random_x, random_y)
    # move_to_element 此名称开头的方法都是将鼠标移动到对应的元素上,而不是页面滚动到对应的元素
    # action.move_to_element(element)
    # 页面滚动到一个元素可以用js
    # browser.execute_script("arguments[0].scrollIntoView();", elemnet)
    action.move_to_element_with_offset(element, random_x, random_y)
    action.click_and_hold(element)
    move_distance = 100
    action.move_by_offset(move_distance, 0)
    action.release().perform()
    # Select
    element_select = browser.find_element_by_xpath('select xpath')
    selector = Select(element_select)
    selector.select_by_index(index)
    selector.select_by_value('value')
    selector.select_by_visible_text('text')
    # 一般的日期控件修改
    js = f'document.getElementById({id}).removeAttribute("readonly");'
    # 或者
    js = f'document.getElementsByName({name})[0].removeAttribute("readonly");'
    browser.execute_script(js)
    js = f'document.getElementById({id}).value="{value}"'
    # 或者
    js = f'document.getElementsByName({name})[0].value="{value}"'
    browser.execute_script(js)
    # 或者
    elememt = browser.find_element_by_id('id')
    # 或者
    element = browser.find_element_by_name('name')
    browser.execute_script('arguments[0].removeAttribute("readonly")', element)
    browser.execute_script(f'arguments[0].value="{value}"', element)
    # 有可能要先通过js把日期值置空,然后通过send_keys('日期')进行日期赋值
    element.send_keys('日期')
    elememt.send_keys(keys.TAB)

 

posted @ 2022-01-19 09:07  liDB  阅读(43)  评论(0编辑  收藏  举报