最新消息:20210917 已从crifan.com换到crifan.org

【已解决】Selenium中Chrome如何判断某个窗口的标题和关闭某个窗口

chrome crifan 3563浏览 0评论

Selenium中用chrome做为driver去点击某个按钮而打开新窗口

然后此处希望:

判断某个窗口的url符合某个条件,则跳转过去

否则就close对应窗口

selenium window handle url

selenium – How to get the URL of page from WebDriver instance that opens in new window – Stack Overflow

WebdriverIO – windowHandle

Selenium Webdriver Switch commands | Selenium Tutorials | ToolsQA

How can I switch to new window using webdriver? – Software Quality Assurance & Testing Stack Exchange

去官网看window的handle,也没发现其他功能:

7. WebDriver API — Selenium-Python中文文档 2 documentation

<code>current_window_handle
Returns the handle of the current window.
Usage:
driver.current_window_handle 

</code>

window_handles

Returns the handles of all windows within the current session.

Usage:

driver.window_handles

利用自动完成,结果发现:

driver.current_window_handle

是没有url的:

只有title:

再去看看源码:

/usr/local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py

<code>@property
def current_window_handle(self):
    """
    Returns the handle of the current window.

    :Usage:
        driver.current_window_handle
    """
    if self.w3c:
        return self.execute(Command.W3C_GET_CURRENT_WINDOW_HANDLE)['value']
    else:
        return self.execute(Command.GET_CURRENT_WINDOW_HANDLE)['value']

@property
def window_handles(self):
    """
    Returns the handles of all windows within the current session.

    :Usage:
        driver.window_handles
    """
    if self.w3c:
        return self.execute(Command.W3C_GET_WINDOW_HANDLES)['value']
    else:
        return self.execute(Command.GET_WINDOW_HANDLES)['value']

</code>

发现果然不是对象,而是个字符串?

看源码:

GET_CURRENT_WINDOW_HANDLE = “getCurrentWindowHandle”

搜:

html getCurrentWindowHandle

GetCurrentWindowHandle

selenium-git-release-candidate/Bridge.html at master · krosenvold/selenium-git-release-candidate

Bridge.html in selenium | source code search engine

发现这个根本不是html的命令

而是selenium自己的命令,然后其他不同web的driver去实现对应的功能?

先不管了,先去调试看看WindowHandle有哪些属性。

结果真的只是一个unicode的字符串而已:

u’CDwindow-(CA2E93A69D8B2C7CDA07EC0BFF88FDCE)’

所以放弃这个方式。

只能去切换到对应的window,然后获取url或title。

即:

driver.switch_to.window(eachWindowHandle)

然后才能:

通过driver.current_url或driver.title获得对应url或title

再去看看,如何关闭某个tab窗口

selenium close current tab

selenium – Is there a way to close a tab in WebDriver or Protractor? – Stack Overflow

Python Selenium – Open new tab / focus tab / close tab

driver.close()

-》看到源码:

/usr/local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py

<code>def close(self):
    """
    Closes the current window.

    :Usage:
        driver.close()
    """
    self.execute(Command.CLOSE)
</code>

所以:是关闭当前窗口,是可以实现我们要的效果的。

所以思路是:

先用

driver.switch_to.window(eachWindowHandle)

然后再调用

driver.close()

去关闭当前窗口。

【总结】

最后用代码:

<code>
newOpenedWindowHandle = None # u'CDwindow-(BCE6B4475385919E3AF77EFEEC2E2015)'
# find the window to jump:
# new opened tab: https://www.befrugal.com/coupons/cashbacksignuppopup/nu/?rtr=3681
# or has redirected : https://www.microsoft.com/en-us/store/b/home?ranMID=24542&amp;ranEAID=cFSwUBVvVPI&amp;ranSiteID=cFSwUBVvVPI-w4lvh_h39HKgSlVfHTnJlA&amp;tduid=(e0fbf07b421743fd5bf0cc4325140b3b)(256380)(2459594)(cFSwUBVvVPI-w4lvh_h39HKgSlVfHTnJlA)()
for eachWindowHandle in allWindowHandles:
    logging.debug("eachWindowHandle=%s", eachWindowHandle)
    driver.switch_to.window(eachWindowHandle)
    if ("coupons/cashbacksignuppopup" in driver.current_url) or ("microsoft.com" in driver.current_url):
        newOpenedWindowHandle = eachWindowHandle
        logging.debug("newOpenedWindowHandle=%s", newOpenedWindowHandle)
        break

# close other tabs/windows
for eachWindowHandle in allWindowHandles:
    logging.debug("eachWindowHandle=%s", eachWindowHandle)
    if eachWindowHandle != newOpenedWindowHandle:
        driver.switch_to.window(eachWindowHandle)
        driver.close()

driver.switch_to.window(newOpenedWindowHandle)
newCurWindowHandle = driver.current_window_handle
logging.debug("newCurWindowHandle=%s", newCurWindowHandle)
logging.info("Switched new open tab: %s", newCurWindowHandle)
</code>

实现了上述需求:

对于多个tab窗口,(先要切换到该窗口)才能获取该窗口的title和url

对于关闭某个窗口,(先要切换到该窗口)再去用close直接关闭。

转载请注明:在路上 » 【已解决】Selenium中Chrome如何判断某个窗口的标题和关闭某个窗口

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
100 queries in 0.196 seconds, using 23.33MB memory