【问题标题】:How to open and manage new tabs with selenium如何使用 selenium 打开和管理新标签页
【发布时间】:2020-09-15 17:11:15
【问题描述】:

我正在尝试使用“https://www.gmail.com”网址打开一个新标签,然后选择一些信息并返回第一页,我正在使用 CTRL + t 命令打开新标签,但是,如何在两个页面之间切换? 我的代码片段:

driver.find_elements_by_css_selector('body').send_keys(Keys.CONTROL + "t")

实际代码:

            bot = self.bot

            guids = bot.window_handles()

            time.sleep(2)

            bot.execute_script("window.open()")

            bot.get('https://gmail.com')

            time.sleep(1)

Python 版本 = Python 3.8.5

【问题讨论】:

    标签: python selenium webdriver selenium-chromedriver


    【解决方案1】:
    #current window
        first_tab = bot.window_handles[0]
    #create new tab
        bot.execute_script("window.open()")
    #move to new tab
        new_tab = bot.window_handles[1]
        bot.switch_to.window(new_tab)
        bot.get('https://gmail.com')
    #switch to first tab
        bot.switch_to.window(first_tab)
    

    【讨论】:

    • 我遇到了这些错误:Instance of 'WebDriver' has no 'getWindowHandles' member,和:Instance of 'WebDriver' has no 'getWindowHandle' member
    • 更新了我的答案 - 它是 driver.window_handles() 和 driver.switch_to_window(guid)
    • 嘿,实际上,我遇到了一个错误:TypeError: 'list' object is not callable
    • 可以这样使用 window_before = driver.window_handles[0] window_after = driver.window_handles[1] driver.switch_to_window(window_after)
    猜你喜欢
    • 2019-10-29
    • 1970-01-01
    • 1970-01-01
    • 2021-10-10
    • 2015-04-10
    • 1970-01-01
    • 1970-01-01
    • 2020-04-09
    相关资源
    最近更新 更多