如何切换到Selenium中的活动选项卡?

Uri*_*Uri 35 python selenium google-chrome-extension

我们开发了Chrome扩展程序,我想用Selenium测试我们的扩展程序.我创建了一个测试,但问题是我们的扩展在安装时会打开一个新标签,我想我从另一个标签中得到了一个例外.是否可以切换到我正在测试的活动选项卡?或者另一种选择是从禁用扩展开始,然后登录我们的网站,然后启用扩展.可能吗?这是我的代码:

def login_to_webapp(self):
    self.driver.get(url='http://example.com/logout')
    self.driver.maximize_window()
    self.assertEqual(first="Web Editor", second=self.driver.title)
    action = webdriver.ActionChains(driver=self.driver)
    action.move_to_element(to_element=self.driver.find_element_by_xpath(xpath="//div[@id='header_floater']/div[@class='header_menu']/button[@class='btn_header signature_menu'][text()='My signature']"))
    action.perform()
    self.driver.find_element_by_xpath(xpath="//ul[@id='signature_menu_downlist'][@class='menu_downlist']/li[text()='Log In']").click()
    self.driver.find_element_by_xpath(xpath="//form[@id='atho-form']/div[@class='input']/input[@name='useremail']").send_keys("[email]")
    self.driver.find_element_by_xpath(xpath="//form[@id='atho-form']/div[@class='input']/input[@name='password']").send_keys("[password]")
    self.driver.find_element_by_xpath(xpath="//form[@id='atho-form']/button[@type='submit'][@class='atho-button signin_button'][text()='Sign in']").click()
Run Code Online (Sandbox Code Playgroud)

测试失败ElementNotVisibleException: Message: element not visible,因为在新选项卡中(由扩展名打开)"登录"不可见(我认为新选项卡仅在命令后打开self.driver.get(url='http://example.com/logout')).

更新:我发现该例外与额外标签无关,它来自我们的网站.但根据@ aberna的回答,我用这段代码关闭了额外的标签:

def close_last_tab(self):
    if (len(self.driver.window_handles) == 2):
        self.driver.switch_to.window(window_name=self.driver.window_handles[-1])
        self.driver.close()
        self.driver.switch_to.window(window_name=self.driver.window_handles[0])
Run Code Online (Sandbox Code Playgroud)

关闭额外标签后,我可以在视频中看到我的标签.

Ava*_*ure 43

这实际上对我有用3.x:

driver.switch_to.window(driver.window_handles[1])
Run Code Online (Sandbox Code Playgroud)

窗口句柄被追加,因此这将选择列表中的第二个选项卡

继续第一个标签:

driver.switch_to.window(driver.window_handles[0])
Run Code Online (Sandbox Code Playgroud)

  • 它看起来很黑,但如果你知道你的测试网站应该在一个新标签中打开一个链接而不是其他任何东西,它就会很好用。非常适合快速和肮脏。 (3认同)
  • @sdkks对不起,我没有明白你的意思。抱歉,如果我的评论愚蠢或令人反感,那不是故意的。我使用了这段代码,它工作正常。您能详细说明为什么它看起来有点黑吗?用* you *这个词我不是说“您,用户sdkks”,而是“被测试的站点应该在一个新选项卡中打开链接”的另一个原因,例如,如果开发人员明确要求Webdriver打开一个新选项卡。 (2认同)

abe*_*rna 31

一些可能的方法:

1 - 使用send_keys(CONTROL + TAB)在选项卡之间切换

self.driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.TAB)
Run Code Online (Sandbox Code Playgroud)

2 - 使用ActionsChains(CONTROL + TAB)在选项卡之间切换

actions = ActionChains(self.driver)      
actions.key_down(Keys.CONTROL).key_down(Keys.TAB).key_up(Keys.TAB).key_up(Keys.CONTROL).perform()
Run Code Online (Sandbox Code Playgroud)

3 - 另一种方法可以使用Selenium方法来检查当前窗口并移动到另一个窗口:

您可以使用

driver.window_handles
Run Code Online (Sandbox Code Playgroud)

找到窗口句柄列表,然后尝试使用以下方法切换.

- driver.switch_to.active_element      
- driver.switch_to.default_content
- driver.switch_to.window
Run Code Online (Sandbox Code Playgroud)

  • 我在使用新选项卡时遇到问题,这种方法挽救了一天`driver.switch_to_window(driver.window_handles[1])` (4认同)

Ped*_*ito 9

接受的答案对我不起作用。
要打开一个新选项卡并使用硒开关,我使用了:

driver.execute_script('''window.open("https://some.site/", "_blank");''')
sleep(1) # you can also try without it, just playing safe
driver.switch_to_window(driver.window_handles[-1]) # last opened tab handle
# driver.switch_to.window(driver.window_handles[-1])  for newer versions.
Run Code Online (Sandbox Code Playgroud)

如果需要切换回主选项卡,请使用:

driver.switch_to_window(driver.window_handles[0])
Run Code Online (Sandbox Code Playgroud)

摘要:

window_handles包含已handles打开的列表tabs,将其用作输入参数switch_to_window()以在选项卡之间切换。

  • 请注意,`switch_to_window()` 已被弃用并替换为 `switch_to.window()` (3认同)

小智 5

ctrl+t或选择window_handles[0]假定您在开始时只打开一个选项卡。

如果您打开了多个选项卡,则它可能会变得不可靠。

这就是我所做的:

old_tabs=self.driver.window_handles
#Perform action that opens new window here
new_tabs=self.driver.window_handles
for tab in new_tabs:
     if tab in old tabs:
          pass
     else:
          new_tab=tab
driver.switch_to.window(new_tab)
Run Code Online (Sandbox Code Playgroud)

这是在切换到新选项卡之前肯定会识别它并将活动窗口设置为所需的新选项卡的东西。

只是告诉浏览器发送ctrl+tab不起作用,因为它不会告诉 webdriver 实际切换到新选项卡。