zoukankan      html  css  js  c++  java
  • selenium使用headless无头模式实现无界面运行

    总所周知,平时使用selenium做web自动化,运行代码的时候都会打开浏览器驱动访问界面。那么有没有什么方式可以实现无界面运行,让我们在调试代码的时候可以屏蔽界面,去做其他事情呢,答案是有的!

    加上以下操作,就能实现无界面运行了。

    先导包:

    from selenium.webdriver.chrome.options import Options

    加入如下配置:

    chrome_options = Options()

    chrome_options.add_argument('--window-size=1920,1080')      # 设置窗口界面大小

    chrome_options.add_argument('--headless')

    driver = webdriver.Chrome(chrome_options=chrome_options)

    参考代码:

    from selenium import webdriver
    import time
    import multiprocessing
    from selenium.webdriver.chrome.options import Options
    
    
    
    class Zutuan():
        def __init__(self):
            """打开浏览器"""
            self.chrome_options = Options()
            self.chrome_options.add_argument('--window-size=1920,1080')
            self.chrome_options.add_argument('--headless')
            self.driver = webdriver.Chrome(chrome_options=self.chrome_options)
    
        def open_zutuan(self, url):
            """传入组团url"""
            self.driver.get(url)
            #self.driver.maximize_window()
            self.driver.refresh()
            #time.sleep(0.01)
            self.driver.implicitly_wait(30)       # todo implicitly隐式等待,等待元素可见
    
        def option_element(self, user, password):
            """xpath定位元素"""
            self.driver.find_element_by_xpath('//div[@class="login a"]/i').click()
            time.sleep(0.01)
            self.driver.find_element_by_xpath('//div[@class="a-title"]').click()
            self.driver.find_element_by_xpath('//input[@type="text" or @class="userName"]').send_keys(user)
            self.driver.find_element_by_xpath('//input[@type="password"]').send_keys(password)
            self.driver.find_element_by_xpath('//div[@class="button"]').click()
            time.sleep(1)
            self.driver.refresh()
    
    
        def select_commodity(self, content):
            """搜索组团商品"""
            # TODO self.content实例属性传给下面的方法使用,如果想把值给下面的方法用,添加实例属性解决
            self.content = content
            self.driver.find_element_by_xpath('//input[@type="text"]').send_keys(content)
            self.driver.find_element_by_xpath('//div[@class="search"]').click()
            self.driver.refresh()
            #return content
    
        def result(self):
            """判断搜索商品成功后的提示信息,断言页面是否成功"""
            if self.content in self.driver.page_source:
                #print(self.content)
                print('商品搜索成功,测试通过')
            else:
                print('商品搜索错误,测试失败')
    
        def closed(self):
            """关闭浏览器"""
            time.sleep(1)
            self.driver.quit()
    
    
    def run1():
        # TODO 根据操作顺序,调用方法执行
        zt = Zutuan()
        zt.open_zutuan('http://www.zutuan.cn/index.html#/')
        zt.option_element('1489088761@qq.com', 'mg123456')
        zt.select_commodity('香蕉')
        zt.result()
        zt.closed()
    
    
    class View_details(Zutuan):
        """把商品添加为明星单品,"""
        def check_commodity(self, number):
            """进入商品详情页,点击添加明星单品"""
            self.driver.find_element_by_xpath('//a[@target="_blank"]/img').click()
            self.driver.switch_to.window(self.driver.window_handles[1])
            self.driver.find_element_by_xpath('//div[@class="child start"]').click()
            self.driver.find_element_by_xpath('//div[@class="el-dialog__body"]//input[@type="text"]').send_keys(number)
            self.driver.find_element_by_xpath('//button[@type="button" and @class="el-button el-button--danger"]').click()
            time.sleep(1)
    
        def result(self):
            """重写父类方法,判断商品添加成功后的提示信息,断言页面是否成功"""
            if '添加成功' in self.driver.page_source:
                print('商品添加成功,测试通过')
            else:
                print('商品添加失败,测试失败')
            # 调用父类方法关闭
            super().closed()
    
    
    def run2():
        vd = View_details()
        vd.open_zutuan('http://www.zutuan.cn/index.html#/')
        vd.option_element('1489088761@qq.com', 'mg123456')
        vd.select_commodity('苹果')
        vd.check_commodity(91628)
        vd.result()
    
    
    def main():
        p1 = multiprocessing.Process(target=run1)
        p2 = multiprocessing.Process(target=run2)
    
        p1.start()
        p2.start()
    
    
    if __name__ == '__main__':
        main()
  • 相关阅读:
    POJ 1300 Open Door
    POJ 2230 Watchcow
    codevs 1028 花店橱窗布置
    codevs 1021 玛丽卡
    codevs 1519 过路费
    codevs 3287 货车运输
    codevs 3305 水果姐逛水果街二
    codevs 1036 商务旅行
    codevs 4605 LCA
    POJ 1330 Nearest Common Ancestors
  • 原文地址:https://www.cnblogs.com/xiamaojjie/p/12041693.html
Copyright © 2011-2022 走看看