selenium--multithreading to start the browser

Here is a simple list of how to start multiple browsers, first list the ideas here, and write step by step according to the ideas

1. First package the browser driver separately, and directly select the corresponding driver when calling

2. Encapsulate the use cases that you want to execute separately.

3. For use cases that are encapsulated through multiple threads, each time a thread is started, the use case is run once.

4. List the names of multiple browsers, and pass the browser parameters into the use case in a circular manner. use case

code show as below:

from selenium import webdriver
import threading
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
import time


# 封装浏览器驱动
def Browser(browser):
    driver = None
    if browser == "ie":
        driver = webdriver.Ie()
    elif browser == "firefox":
        binary = FirefoxBinary(r'C:\Program Files\Mozilla Firefox\firefox.exe')
        driver = webdriver.Firefox(firefox_binary=binary)
        # driver = webdriver.Firefox()
    elif browser == "chrome":
        driver = webdriver.Chrome()
    else:
        print('输入的正确的浏览器信息')
    return driver


# 封装用例
def test_baidu(browser_name):
    driver = Browser(browser_name)
    driver.get('https://www.baidu.com')
    driver.find_element_by_id('kw').send_keys('yx_test')
    driver.find_element_by_id('su').click()
    print(driver.title)
    print('通过浏览器:', browser_name)
    time.sleep(2)
    driver.quit()


# 封装多线程
def run_case(name):
    thread_list = []
    for i in range(1):
        appium_server = threading.Thread(target=test_baidu, args=(name,))
        thread_list.append(appium_server)
    for j in thread_list:
        j.start()


if __name__ == '__main__':
    a = ('chrome', 'firefox')
    for i in a:
        run_case(i)

operation result:

Open Google and Firefox browsers respectively, and enter content to search

insert image description here

Problems encountered and solutions:

insert image description here

Guess you like

Origin blog.csdn.net/weixin_45422695/article/details/130871027