简体   繁体   English

如何在webdriver(Python)中打开一个新窗口或选项卡?

[英]How to open a new window or tab in webdriver (Python)?

I have tried various commands like 我尝试了各种各样的命令

driver.switch_to_window("_blank")

or 要么

driver.switch_to_window(None)

but none of these worked. 但这些都没有奏效。 How is this supposed to be done? 应该怎么做? Thanks. 谢谢。

After tons of googling, I came across what works for me in Firefox and which is so simple (elegant, maybe?) as to be almost laughable. 经过大量的谷歌搜索,我发现在Firefox中对我有用的东西是如此简单(优雅,可能?)几乎可笑。 Here it is: 这里是:

>>> from selenium import webdriver
>>> driver = webdriver.Firefox()
>>> driver.execute_script("window.open('');")  <--- JAVASCRIPT!

Look at this question . 看看这个问题 Seems like the library doesn't support tab management atm. 好像图书馆不支持标签管理atm。

But the second-highest rated answer does offer a solution. 但排名第二的答案确实提供了解决方案。 I think you can easily translate it to Python. 我想你可以轻松地将它翻译成Python。

I have solved the problem this way: 我用这种方式解决了这个问题:

a_elem = driver.find_element_by_link_text("Link with _blank")
self.driver.get(a_elem.get_attribute('href'))

I ran into the same issue, and I wasn't really satisfied with other solutions I found, so I made a context manager to do the handling. 我遇到了同样的问题,我对我找到的其他解决方案并不满意,所以我做了一个上下文管理器来处理。 This allows: 这允许:

with link_in_new_window(driver, link_element):
    driver.foo()  # this is in the new window opened from the link
driver.foo()  # this is back in the original window

..which I find to be very convenient. ..我觉得很方便。

from contextlib import contextmanager
from selenium.common import exceptions
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys

# Fundamentally, this shift-clicks to open a new window, and
# hits ctrl-w to close it when done, using ActionChains.
@contextmanager
def link_in_new_window(driver, element):
    """Open a clickable element in a new window for this context

    This opens a clickable element in a new window, and switches
    `driver` to that window for the duration of the context.  
    The window is closed afterward, and the driver switched back
    to the original window.

    If the 'as' clause is used, the handle of the parent (original)
    window is provided as the value.

    Selenium doesn't provide a useful interface to firefox's tabs
    due to some technical complications, so we use new windows
    instead of new tabs. The memory hit isn't too significant.

    :type driver: selenium.webdriver.Firefox
    :type element: selenium.webdriver.remote.webelement.WebElement
    :returns: Parent's Window Handle
    """

    known_handles = set(driver.window_handles)
    original_handle = driver.current_window_handle
    action = ActionChains(driver)
    # Open link in new window (shift-click element)
    # Sometimes, shift would be ignored. move_to_element helped.
    action.move_to_element(element)
        .key_down(Keys.SHIFT)
        .click(element)
        .key_up(Keys.SHIFT)
        .perform()

    # Find the new window handle.  ..anyone know a better way?
    timeout = time.time() + 10
    new_handles = set(driver.window_handles) - known_handles
    while time.time() < timeout and not new_handles:
        new_handles = set(driver.window_handles) - known_handles
        time.sleep(0.5)
    if not new_handles or len(new_handles) != 1:
        msg = "Expected one handle, found {}"
        raise RuntimeError(msg.format(len(new_handles)), new_handles)
    new_handle = new_handles.pop()
    driver.switch_to.window(new_handle)
    try:
        # User code will be executed here
        yield original_handle
    finally:
        # close window, if it still exists (ctrl-w)
        try:
            driver.switch_to.window(new_handle)
            action = ActionChains(driver)
            action.key_down(Keys.CONTROL).send_keys('w').perform()
        except exceptions.NoSuchWindowException:
            pass
        # in any case, switch back to the original window
        driver.switch_to.window(original_handle)

暂无
暂无

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

相关问题 在Python中使用Selenium WebDriver在新标签页/窗口中打开链接 - Using Selenium WebDriver in Python to Open Link in New Tab/Window Python + Selenium WebDriver:在新标签页中打开URL - Python + Selenium WebDriver: open URL in new tab 如何使用python的Selenium WebDriver在浏览器上打开一个新的window? - How to open a new window on a browser using Selenium WebDriver for python? 如何使用 Control + Click of Selenium Webdriver 在同一窗口的新选项卡中的主选项卡中打开嵌入在 webelement 中的链接 - How to open a link embeded in a webelement with in the main tab, in a new tab of the same window using Control + Click of Selenium Webdriver 如何在 splinter webdriver python 中切换到窗口或选项卡? - How to switch to window or tab in splinter webdriver python? Python - 使用 Selenium WebDriver 在新的 Chrome 选项卡中打开链接? - Python - Open a link in new Chrome tab with Selenium WebDriver? Selenium webdriver在python中的chrome编码中打开一个新选项卡 - Selenium webdriver open a new tab in chrome coding in python python selenium webdriver打开新窗口句柄等待 - python selenium webdriver open new window handle wait 如何在python中编辑url并打开新页面而不打开新窗口或标签? - How do I edit the url in python and open a new page without having a new window or tab opened? 如何在Python的Selenium Webdriver的新选项卡中禁用打开页面? - How to disable opening the page in a new tab in Selenium Webdriver in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM