zoukankan      html  css  js  c++  java
  • selenium +chrome headless Adhoc模式渲染网页

    mannual和adhoc模式比较

    Manual vs. Adhoc

    In the script above, we start the ChromeDriver server process when we create the WebDriver object and it is terminated when we call quit(). For a one-off script, that isn’t a problem, but this can waste a nontrivial amount of time for a large test suite that creates a ChromeDriver instance for each test. Luckily, we can manually start and stop the server ourselves, and it only requires a few changes to the script above.

    说的在使用selenium+chromeheadless做自动化测试时候,如果测试用例数量大,给每个测试用例启动一个浏览器要话费更多的时间。

    #coding=utf8
    import os,traceback
    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    from selenium.webdriver.chrome.options import Options

    service = webdriver.chrome.service.Service('/home/xxxx/Downloads/phantomjs-2.1.1-linux-x86_64/bin/chromedriver')
    service.start()
    class ChromeHeadless(object):

    def __init__(self):

    chrome_options = Options()
    chrome_options.add_argument("--headless")
    chrome_options.add_argument('--disable-gpu')
    chrome_options.add_argument('--disable-images')
    chrome_options.add_argument('--disable-plugins')
    chrome_options.binary_location = '/opt/google/chrome/chrome'
    #prefs = {"profile.managed_default_content_settings.images": 2} # 关图片
    #chrome_options.add_experimental_option("prefs", prefs)
    self.chrome_options=chrome_options
    self.driver = webdriver.Remote(service.service_url,desired_capabilities=self.chrome_options.to_capabilities())
    self.driver.set_page_load_timeout(120)

    def fun(self,url):

    try:
    proxy_http_list = list(r.smembers('kuaidaili:http')) + list(r.smembers('zhima'))
    pr=random.choice(proxy_http_list)
    self.chrome_options.add_argument('--proxy-server=%s'%pr) #设置代理
    self.driver.start_session(self.chrome_options.to_capabilities())
    #print self.driver.session_id
    self.driver.get(url)

    except Exception,e:

    print '33[7;32;0m%s33[0m' %traceback.format_exc()




  • 相关阅读:
    Windows server 2008 R2充当路由器实现网络的互联(转)
    sqlserver内存释放心得
    收藏一个好用的base64编解码网址
    最后一篇,说好的开源来了!
    python五子棋
    flask使用原生ajax、不使用表单(Form)上传文件
    使用keras的LSTM进行预测----实战练习
    keras神经网络三个例子
    【强化学习】用pandas 与 numpy 分别实现 q-learning, saras, saras(lambda)算法
    【转】【强化学习】Deep Q Network(DQN)算法详解
  • 原文地址:https://www.cnblogs.com/ydf0509/p/7340854.html
Copyright © 2011-2022 走看看