zoukankan      html  css  js  c++  java
  • selenium元素和浏览器操作

    • click和clear
    from selenium.webdriver.support.wait import WebDriverWait
    import time
    
    browser = webdriver.Chrome()
    browser.get('https://www.baidu.cn/')
    wait = WebDriverWait(browser, 10)
    
    input_tag = wait.until(EC.presence_of_element_located((By.ID, 'twotabsearchtextbox')))
    input_tag.send_keys('iphone 8')
    button = browser.find_element_by_css_selector('#nav-search > form > div.nav-right > div > input')
    button.click()
    time.sleep(3)
    
    input_tag = browser.find_element_by_id('twotabsearchtextbox')
    input_tag.clear()  # 清空输入框
    input_tag.send_keys('iphone7plus')
    button = browser.find_element_by_css_selector('#nav-search > form > div.nav-right > div > input')
    button.click()
    
    • ActionChains
    from selenium import webdriver
    from selenium.webdriver import ActionChains
    from selenium.webdriver.support.wait import WebDriverWait
    import time
    
    driver = webdriver.Chrome()
    driver.get('http://www.runoob.com/try/try.php?filename=jqueryui-api-droppable')
    wait = WebDriverWait(driver, 3)
    
    try:
        driver.switch_to.frame('iframeResult')  # 切换到iframeResult
        sourse = driver.find_element_by_id('draggable')
        target = driver.find_element_by_id('droppable')
    
        # 方式一:基于同一个动作链串行执行
        # actions=ActionChains(driver) #拿到动作链对象
        # actions.drag_and_drop(sourse,target) #把动作放到动作链中,准备串行执行
        # actions.perform()
    
        # 方式二:不同的动作链,每次移动的位移都不同
        ActionChains(driver).click_and_hold(sourse).perform()
        distance = target.location['x'] - sourse.location['x']
    
        track = 0
        while track < distance:
            ActionChains(driver).move_by_offset(xoffset=2, yoffset=0).perform()
            track += 2
    
        ActionChains(driver).release().perform()
    
        time.sleep(10)
    
    finally:
        driver.close()
    
    • 自定义JS
    from selenium import webdriver
    
    try:
        browser = webdriver.Chrome()
        browser.get('https://www.baidu.com')
        browser.execute_script('alert("hello world")')  # 打印警告
    finally:
        browser.close()
    
    
    • 切换iframe
    from selenium import webdriver
    
    try:
        browser = webdriver.Chrome()
        browser.get('http://www.runoob.com/try/try.php?filename=jqueryui-api-droppable')
    
        browser.switch_to.frame('iframeResult')  # 切换到id为iframeResult的frame
    
        tag1 = browser.find_element_by_id('droppable')
        print(tag1)
    
        # tag2=browser.find_element_by_id('textareaCode') #报错,在子frame里无法查看到父frame的元素
        browser.switch_to.parent_frame()  # 切回父frame,就可以查找到了
        tag2 = browser.find_element_by_id('textareaCode')
        print(tag2)
    
    finally:
        browser.close()
    
    • 显示等待和隐式等待
      • 隐式等待:在browser.get('xxx')前就设置,针对所有元素有效
    browser.implicitly_wait(10)
    
    • 显式等待:在browser.get('xxx')之后设置,只针对某个元素有效
    from selenium.webdriver.support.wait import WebDriverWait
    wait=WebDriverWait(browser,10)
    wait.until(EC.presence_of_element_located((By.ID,'content_left')))
    
    • 获取标签属性
    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
    
    browser = webdriver.Chrome()
    browser.get('https://www.amazon.cn/')
    
    wait = WebDriverWait(browser, 10)
    wait.until(EC.presence_of_element_located((By.ID, 'cc-lm-tcgShowImgContainer')))
    tag = browser.find_element(By.CSS_SELECTOR, '#cc-lm-tcgShowImgContainer img')
    
    # 获取标签属性,
    print(tag.get_attribute('src'))
    
    # 获取标签ID,位置,名称,大小
    print(tag.id)
    print(tag.location)
    print(tag.tag_name)
    print(tag.size)
    
    browser.close()
    

    浏览器操作

    • 浏览器前进后退
    browser.back()
    browser.forward()
    
    • cookies
    from selenium import webdriver
    
    browser=webdriver.Chrome()
    browser.get('https://www.zhihu.com/explore')
    print(browser.get_cookies())
    browser.add_cookie({'k1':'xxx','k2':'yyy'})
    print(browser.get_cookies())
    
    # browser.delete_all_cookies()
    
    • 切换选项卡
    import time
    from selenium import webdriver
    
    browser=webdriver.Chrome()
    browser.get('https://www.baidu.com')
    browser.execute_script('window.open()')
    
    print(browser.window_handles) #获取所有的选项卡
    browser.switch_to_window(browser.window_handles[1])
    browser.get('https://www.taobao.com')
    time.sleep(10)
    browser.switch_to_window(browser.window_handles[0])
    browser.get('https://www.sina.com.cn')
    browser.close()
    
    • 异常处理
    from selenium import webdriver
    from selenium.common.exceptions import TimeoutException,NoSuchElementException,NoSuchFrameException
    
    try:
        browser=webdriver.Chrome()
        browser.get('http://www.runoob.com/try/try.php?filename=jqueryui-api-droppable')
        browser.switch_to.frame('iframssseResult')
    
    except TimeoutException as e:
        print(e)
    except NoSuchFrameException as e:
        print(e)
    finally:
        browser.close()
    
  • 相关阅读:
    Oracle死锁
    Oracle forall bulk collect批量数据更新
    Oracle组合索引与回表
    查看Oracle执行计划
    Oracle 用户、角色、权限(系统权限、对象权限)的数据字典表
    Oracle计算两个时间戳相差秒数,精确到毫秒
    关于Oracle的疑问
    Oracle update和order by
    Oracle Connect by与递归with
    关于Oracle表连接
  • 原文地址:https://www.cnblogs.com/iamluoli/p/11528714.html
Copyright © 2011-2022 走看看