相关疑难解决方法(0)

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

我们开发了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)

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

python selenium google-chrome-extension

35
推荐指数
4
解决办法
5万
查看次数

标签 统计

google-chrome-extension ×1

python ×1

selenium ×1