• 【自动化测试】WebDriver使用


    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    from bs4 import BeautifulSoup
    from selenium.webdriver.chrome.options import Options
    
    import time
    import selenium
    import random
    
    
    
    # from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
    
    def driver_open():
        # dcap = dict(DesiredCapabilities.PHANTOMJS)
        # dcap["phantomjs.page.settings.userAgent"] = (r"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3100.0 Safari/537.36")
        options = Options()
        options.add_argument('--log-level=3')
        driver = webdriver.Chrome(executable_path='E:chromedriverchromedriver.exe', chrome_options=options)
        return driver
    
    def next_windows(driver,current_window):
        window_handles = driver.window_handles
        for window_handle in window_handles:
            if current_window != window_handle:
                handleb = window_handle
    
        driver.switch_to.window(handleb)
        return  driver
    
    def next_windows_handle(driver,current_window):
        window_handles = driver.window_handles
        for window_handle in window_handles:
            if current_window != window_handle:
                handleb = window_handle
        return handleb
    
    if __name__ == '__main__':
        learn_dict = {}
        driver = driver_open()
        driver.maximize_window()
        driver.implicitly_wait(10)  # seconds
        # # Theoretical study
        theoretical_study_url = 'xxxxx'
        driver.get(theoretical_study_url)
        time.sleep(20)
        current_window = driver.current_window_handle
        print(current_window)
        # 浏览6篇文章
        article = 6
        elements = driver.find_elements_by_xpath('//span[contains(@class,"text")]')
        for element in elements:
            element.click()
            driver = next_windows(driver,current_window)
            client_height = driver.execute_script("return document.body.clientHeight;")
            # scroll_height = driver.execute_script("return document.body.scrollWidth;")
    
            # print(scroll_height)
            init_position = 0
            while (client_height > init_position):
                init_position = init_position + 200
                js_string = "window.scrollTo(0," + str(init_position) + ");"
                driver.execute_script(js_string)
                sleep_time = random.randint(2, 4)
                time.sleep(sleep_time)
    
            time.sleep(3)
            driver.close()
            driver.switch_to.window(current_window)
            article = article - 1
            if(article == 0):
                break
    
        driver.get('xxxxxx')
        time.sleep(3)
    
        window_handles = driver.window_handles
        for window_handle in window_handles:
            current_window = window_handle
    
        driver.switch_to.window(current_window)
        # 浏览6个视频
        videos_count = 6
        videos = driver.find_elements_by_xpath('//div[contains(@class,"innerPic")]')
        for video in videos:
            driver.switch_to.window(current_window)
            video.click()
            driver = next_windows(driver, current_window)
            js_string = "window.scrollTo(0,400);"
            driver.execute_script(js_string)
            time_sleep = random.randint(10,50)
            time.sleep(time_sleep)
            driver.close()
            videos_count = videos_count - 1
            if(videos_count == 0):
                break
    
        driver.quit()
    

      

  • 相关阅读:
    JDBC编程步奏、问题总结(一)
    基于注解的Spring AOP的配置和使用--转载
    什么是aop?-------转
    The JSP specification requires that an attribute name is preceded by whitespace--异常
    使用注解来构造IoC容器-转
    Java单元测试(Junit+Mock+代码覆盖率)---------转
    Linux 杂项
    Sequence
    vim编辑器高亮sv语法
    linux 权限 chmod 755
  • 原文地址:https://www.cnblogs.com/huixuexidezhu/p/11124441.html
Copyright © 2020-2023  润新知