简体   繁体   English

Python selenium 切换到 window 与特定 ZE6B391A8D2C4D45902A23A8B6DZ857

[英]Python selenium switch to window with specific URL

I have a python code with selenium that opens multiple windows.我有一个 python 代码,其中 selenium 打开多个 windows。 I know how to switch between them numerically but is there a way to switch to a specific tab that's already open by searching the URL?我知道如何在数字上切换它们,但是有没有办法通过搜索 URL 切换到已经打开的特定选项卡?

In Java browser.switchWindow(urlOrTitleToMatch) can do the trick,在 Java browser.switchWindow(urlOrTitleToMatch)可以做到这一点,
where urlOrTitleToMatch is your URL string or a regular expression.其中urlOrTitleToMatch是您的 URL 字符串或正则表达式。 https://webdriver.io/docs/api/browser/switchWindow.html https://webdriver.io/docs/api/browser/switchWindow.html

Edit编辑

In Python it looks like you have to loop through all windows until you find a URL match.在 Python 中,您似乎必须遍历所有 windows 直到找到 URL 匹配项。

import re

def switchWindow(URL, browser):
  for window in browser.getWindowHandles():
    browser.switch_to_window(window)
    if re.search(URL, browser.current_url):
      break;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM