zoukankan      html  css  js  c++  java
  • python selenium T1

    python selenium T1

    EG1 :

      

    import time
    
    from selenium import webdriver
    
    url = 'http://baidu.com'
    urls = ['http://zhidao.baidu.com', 'http://news.baidu.com']
    driver = webdriver.Chrome()
    driver.get(url)
    driver.maximize_window()
    time.sleep(2)
    driver.set_window_size(480, 800)  # width , heigh
    time.sleep(2)
    for u in urls:
        driver.get(u)
        time.sleep(1)
    
    print('%-20s' % ('NOW : ',), driver.current_url)
    driver.back()
    print('%-20s' % ('back 1 : ',), driver.current_url)
    driver.forward()
    print('%-20s' % ('forward 1 : ',), driver.current_url)
    
    driver.quit()
    

    Result

    NOW :                http://news.baidu.com/
    back 1 :             http://news.baidu.com/
    forward 1 :          https://zhidao.baidu.com/
    

      

    EG2 :

    lass OpWebdriver():
        def __init__(self):
            self.driver = driver = webdriver.Chrome()
    
        def login(self):
            login_url = self.login_check()
            if not login_url:
                print('NOW LOGIN ')
            else:
                self.driver.get(login_url)
                self.get_ele(['j_username', 'j_password', 'yui-gen1-button'], ['XXX', 'XXXX'])
    
            ele = self.driver.find_element_by_link_text('log out')
            if ele:
                print('LOGIN DONE')
    
            WebdriverUtils.print_ele('ele size', ele.size,
                                     'ele text', ele.text,
                                     'ele get_attribute', ele.get_attribute('text'),
                                     'ele is_displayed', ele.is_displayed()
    
                                     )
    
        def login_check(self):
            url = 'http://jenkins.banya.com.cn:8080/'
            self.driver.get(url)
            ele = self.driver.find_element_by_partial_link_text('log in')
            if ele:
                return ele.get_attribute('href')
    
        def get_ele(self, eles, values):
    
            for index, ele_id in enumerate(eles[:-1]):
                ele = self.driver.find_element_by_name(ele_id)
                ele.clear()
                ele.send_keys(values[index])
                time.sleep(0.5)
    
            self.driver.find_element_by_id(eles[-1]).submit()
    
    
    op = OpWebdriver()
    op.login()
    

    Result 

    LOGIN DONE
     ele size             {'width': 43, 'height': 15} 
     ele text             log out              
     ele get_attribute    log out              
     ele is_displayed     True       
    

      

    EG3 :

    from selenium import webdriver
    from selenium.webdriver.common.action_chains import ActionChains
    
    url = 'http://baidu.com'
    
    driver = webdriver.Chrome()
    driver.get(url)
    
    driver.find_element_by_name('wd').send_keys('头条')
    ele = driver.find_element_by_id('su')
    target = driver.find_element_by_id('wd')
    ActionChains(driver).context_click(ele).perform()   # 右击
    time.sleep(1)
    ActionChains(driver).double_click(ele).perform()    #  双击
    time.sleep(1)
    ActionChains(driver).drag_and_drop(ele , target).perform()  # 拖动
    time.sleep(1)
    ActionChains(driver).move_to_element(ele).perform() # 鼠标移动到元素上
    time.sleep(1)
    ActionChains(driver).click_and_hold(ele).perform()  # 按下鼠标左键
    

      

    EG4:

    import time
    
    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    
    url = 'http://baidu.com'
    
    driver = webdriver.Chrome()
    driver.get('http://news.baidu.com')
    
    driver.get(url)
    now_handel = driver.current_window_handle
    
    handles = driver.window_handles
    
    
    ele = driver.find_element_by_name('wd')
    ele.send_keys('头条A')
    driver.implicitly_wait(30)
    print('driver.implictly done')
    time.sleep(1)
    ele.send_keys(Keys.BACK_SPACE)
    time.sleep(1)
    ele.send_keys(Keys.CONTROL , 'a')
    time.sleep(1)
    ele.send_keys(Keys.CONTROL,'c')
    time.sleep(1)
    ele.send_keys(Keys.CONTROL,'x')
    time.sleep(1)
    ele.send_keys(Keys.CONTROL,'v')
    
    print(driver.title)
    print(driver.current_url)
    

      

    分页、下拉框、警告框、上传、下载文件、控制浏览器滚动条、Cookie

    关于跳过验证码, 可以通过driver.add_cookie()

  • 相关阅读:
    COLLABNET 在中文语言下无法编辑用户信息.
    安装ramdisk有可能使xp3389不能用
    关于23种设计模式的有趣见解
    成绩统计分析系统规划
    编程用开源软件或者免费软件
    ROS设置大全
    禁用TextBox自动填充autocomplete=false
    CollabNet Subversion Server安装与配置
    DbEntry查询表的使用
    BugTracker.Net设置问题
  • 原文地址:https://www.cnblogs.com/zsr0401/p/6483528.html
Copyright © 2011-2022 走看看