如何使用 python +selenium 打开多个具有相同 url 的浏览器选项卡

How to open multiple browser tab that has the same url using python +selenium

我正在使用 python 和 selenium 进行负载测试。我需要做的是搜索一个系统并显示它的事件内容。目前我有:

test_url = base_url + args.system + event_page_url_addon

问题是,如果我有 5 个线程正在进行此事件,它们具有相同的 URL,因为它们都在寻找相同的系统,并且我有 5 个不同的选项卡,它们都具有相同的 URL。我的问题是如何引用每个 tab/thread? ( tab1, tab2....) 如果我通过在末尾添加索引号来操纵 URL,则该站点变得无法搜索。

感谢任何帮助。

我不确定你为什么要使用 Selenium 进行负载测试,你可能想看看 jmeter

话虽如此,对于此特定任务,您可以在选项卡之间切换。您可以从 Selenium 中的任何选项卡转到任何选项卡。

我假设您点击了第一个选项卡上的某处,然后一定打开了一个新选项卡,对吗?

如果是这样,您可以使用下面的示例代码:

driver.maximize_window()
driver.implicitly_wait(30)
driver.get("Your URL here")
wait = WebDriverWait(driver, 10)
first_tab_handle = driver.current_window_handle  #Storing the current or first tab windows handle
driver.find_element_by_id('some id').click() # the moment this click take place, you would see a new tab with some other or with same content (Assuming this click triggers an event)
two_tabs_handles = driver.window_handles   # Note that, two_tabs_handles will have previous tab (first tab) as well as second tab handles. 
driver.switch_to.window(two_tabs_handles[1])  #  Note that, window_handles returns a list in python, so [0] denotes the first tab whereas [1] denotes the first tab. 

#perfrom some operation here on tab 2 (e.g click somewhere to open tab3 using selenium)

three_tabs_handles = driver.window_handles 
driver.switch_to.window(two_tabs_handles[2])

and so on... 

PS :- 阅读评论以便更好地理解。