在RSelenium中打开一个新标签

时间:2014-10-25 03:56:06

标签: r selenium selenium-webdriver

如何在RSelenium中打开新标签?具体来说,如何指定控制键以发送" CTRL + T"到窗口?我尝试了:

require(RSelenium)
RSelenium::startServer()
dr = remoteDriver()
dr$open()
b = remDr$findElement(using = 'tag', value = "body")
b$sendKeysToElement(list("CONTROL + T")) #this does not work

1 个答案:

答案 0 :(得分:5)

RSelenium有一个键盘键列表,请参阅?selKeys

您可以按如下方式在链接上打开新标签页:

library(RSelenium)
RSelenium::startServer()
dr <- remoteDriver()
dr$open()
dr$navigate("http://www.stackoverflow.com")
# find the Users tab
webElem <- dr$findElement("id", "nav-users")
dr$mouseMoveToLocation(webElement = webElem) # move to the required element
dr$click(2) # right mouse button click 
webElem$sendKeysToElement(list(key = "control", "t")) # open a new tab by sending ctrl+t

Selenium不支持标签,但请参见https://code.google.com/p/selenium/issues/detail?id=5572,因此最好在新窗口中打开

webElem$sendKeysToElement(list(key = "control", "w"))
> dr$getWindowHandles()
[[1]]
[1] "{64da9f4a-4974-4e11-a078-35785ac31227}"
[2] "{952d4b9c-9955-4233-a048-d2e9b043117c}"

> dr$getCurrentWindowHandle()
[[1]]
[1] "{64da9f4a-4974-4e11-a078-35785ac31227}"

> dr$switchToWindow("{952d4b9c-9955-4233-a048-d2e9b043117c}")
> dr$getCurrentWindowHandle()
[[1]]
[1] "{952d4b9c-9955-4233-a048-d2e9b043117c}"