• 鼠标操作


    '''
    鼠标操作
    context_click 右键操作
    drag_and_drop 拖拽操作,拖动某一个元素到另外一个区域,然后释放按键
    move_to_element() 鼠标悬停
    perform() 执行鼠标操作
    引入 ActionChains类
    from selenium.webdriver.common.action_chains import ActionChains
    AC.方法名 1()....perform()

    select类-下拉框操作
    selenium提供了select类来处理selet/option
    引入类
    from selenium.webdriver.support.ui import Select
    选择下拉列表值
    1.通过下标选择:select_by_index(index)从0开始
    2.通过value属性:select_by_value(value值)
    3.通过文本内容:select_by_visible_text(文本内容)
    '''
    from selenium.webdriver.support.ui import Select#引入操作下拉框的类
    from selenium.webdriver.common.action_chains import ActionChains
    from selenium.webdriver.support.wait import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.by import By
    import time
    from selenium import webdriver
    # zfwx = webdriver.Chrome()
    # zfwx.get("http://www.zfwx.com")
    # #1.先定位要操作的元素
    # ele=zfwx.find_element_by_xpath('//a[@class="menu-list-first"]')
    # sj=zfwx.find_element_by_xpath('//li[@class="listul-li"]//a[text()="司法鉴定"]')
    # #2.调用鼠标操作方法最用调用perform()方法执行鼠标操作
    # ActionChains(zfwx).move_to_element(ele).click(sj).perform()#悬浮后可以直接点击操作
    # # zfwx.find_element_by_xpath('//li[@class="listul-li"]//a[text()="司法鉴定"]').click()

    baidu = webdriver.Chrome()
    baidu.get("http://www.baidu.com")
    #1.先定位要操作的元素
    ele=baidu.find_element_by_xpath('//div[@id="u1"]//a[@name="tj_settingicon"]')

    #2.调用perform()执行鼠标操作
    ActionChains(baidu).move_to_element(ele).perform()

    #3.等待元素出现
    loc = (By.XPATH,'//a[text()="高级搜索"]')
    WebDriverWait(baidu,10).until(EC.visibility_of_element_located(loc))


    #4.出现后点击高级搜索(两种点击方式)
    #baidu.find_element_by_xpath('//a[text()="高级搜索"]').click()
    baidu.find_element(loc[0],loc[1]).click()#直接调用定位表达式

    #下拉框处理 == sekect/option这样的元素 使用webdriver Select 类来处理

    #1.找到select元素
    WebDriverWait(baidu,15).until(EC.visibility_of_element_located((By.XPATH,'//select[@name="ft"]')))
    sel=baidu.find_element_by_xpath('//select[@name="ft"]')
    #2.实例化select类
    s= Select(sel)
    #3.选择一个option值 ==通过下标查找
    s.select_by_index(3)
    time.sleep(2)
    #4.通过text
    s.select_by_visible_text('RTF 文件 (.rtf)')
    time.sleep(2)
    #5.通过value
    s.select_by_value('all')
    time.sleep(2)
    # baidu.quit()
  • 相关阅读:
    java。equal()和== 的区别
    java。封装
    java。OOA,OOD,OOR
    java。类和对象
    java、数组;堆区,栈区
    java。 break和continue区别
    NYOJ 228 士兵杀敌(五)【差分标记裸题】
    2017CCPC 杭州 J. Master of GCD【差分标记/线段树/GCD】
    CF1025B Weakened Common Divisor【数论/GCD/思维】
    网络流算法笔记
  • 原文地址:https://www.cnblogs.com/wfwt180801-/p/11126702.html
Copyright © 2020-2023  润新知