我已经在网络和WebDriver API中搜索过了。我没有看到使用WebDriver/Selenium2.0打开新标签页的方法。
能有人确认一下我的想法吗?
谢谢, Chris。 附言:我目前看到的替代方法是要么在同一窗口加载不同的URL,要么打开新窗口。
我已经在网络和WebDriver API中搜索过了。我没有看到使用WebDriver/Selenium2.0打开新标签页的方法。
能有人确认一下我的想法吗?
谢谢, Chris。 附言:我目前看到的替代方法是要么在同一窗口加载不同的URL,要么打开新窗口。
完全有一种跨浏览器的方法可以使用webdriver来实现这一点,那些说你不能的人只是太懒惰了。首先,你需要使用WebDriver将锚标签注入到打开所需选项卡的页面中。以下是我的做法(注意:driver是一个WebDriver实例):
/**
* Executes a script on an element
* @note Really should only be used when the web driver is sucking at exposing
* functionality natively
* @param script The script to execute
* @param element The target of the script, referenced as arguments[0]
*/
public void trigger(String script, WebElement element) {
((JavascriptExecutor)driver).executeScript(script, element);
}
/** Executes a script
* @note Really should only be used when the web driver is sucking at exposing
* functionality natively
* @param script The script to execute
*/
public Object trigger(String script) {
return ((JavascriptExecutor)driver).executeScript(script);
}
/**
* Opens a new tab for the given URL
* @param url The URL to
* @throws JavaScriptException If unable to open tab
*/
public void openTab(String url) {
String script = "var d=document,a=d.createElement('a');a.target='_blank';a.href='%s';a.innerHTML='.';d.body.appendChild(a);return a";
Object element = trigger(String.format(script, url));
if (element instanceof WebElement) {
WebElement anchor = (WebElement) element; anchor.click();
trigger("var a=arguments[0];a.parentNode.removeChild(a);", anchor);
} else {
throw new JavaScriptException(element, "Unable to open tab", 1);
}
}
接下来,您需要告诉webdriver将当前窗口句柄切换到新标签页。以下是我的做法:
/**
* Switches to the non-current window
*/
public void switchWindow() throws NoSuchWindowException, NoSuchWindowException {
Set<String> handles = driver.getWindowHandles();
String current = driver.getWindowHandle();
handles.remove(current);
String newTab = handles.iterator().next();
locator.window(newTab);
}
完成之后,您可以使用相同的WebDriver实例与新页面上下文中的元素进行交互。完成该选项卡后,您始终可以使用类似于上面的switchWindow功能的机制返回默认的窗口上下文。我将把它作为一个让您自己去解决的练习。
switchWindow()
函数中最后一行的locator
是什么? - jamesfzhangpublic static IJavaScriptExecutor Scripts(this IWebDriver driver)
{
return (IJavaScriptExecutor)driver;
}
- JabberwockyDecompiler目前,Selenium WebDriver API 不支持在浏览器内管理选项卡。
var windowHandles = webDriver.WindowHandles;
var script = string.Format("window.open('{0}', '_blank');", url);
scriptExecutor.ExecuteScript(script);
var newWindowHandles = webDriver.WindowHandles;
var openedWindowHandle = newWindowHandles.Except(windowHandles).Single();
webDriver.SwitchTo().Window(openedWindowHandle);
我曾经遇到了同样的问题,并找到了答案。可以试一下。
Robot r = new Robot();
r.keyPress(KeyEvent.VK_CONTROL);
r.keyPress(KeyEvent.VK_T);
r.keyRelease(KeyEvent.VK_CONTROL);
r.keyRelease(KeyEvent.VK_T);
嘿@Paul和所有在Python中打开第二个标签页遇到问题的人。这是解决方案。
我不确定这是否是webdriver中的错误,还是因为它尚未与mutlitab兼容,但它肯定在使用mutlitab时表现不正常,我将展示如何修复它。
问题: 我看到不止一个问题。
第一个问题与当您打开第二个标签页时,您只能看到一个句柄而不是两个有关。
第二个问题,这就是我的解决方案所在的地方。似乎虽然句柄值仍存储在驱动程序中,但窗口由于某种原因已经失去了与其同步。
以下是通过解决第二个问题来解决问题的方法:
elem = browser.find_element_by_xpath("/html/body/div[2]/div[4]/div/a") #href link
time.sleep(2)
elem.send_keys(Keys.CONTROL + Keys.RETURN + "2") #Will open a second tab
#solution for the 2nd issue is here
for handle in browser.window_handles:
print "Handle is:" + str(handle) #only one handle number
browser.switch_to_window(handle)
time.sleep(3)
#Switch the frame over. Even if you have switched it before you need to do it again
browser.switch_to_frame("Frame")
"""now this is how you handle closing the tab and working again with the original tab"""
#again switch window over
elem.send_keys(Keys.CONTROL + "w")
for handle in browser.window_handles:
print "HandleAgain is:" + str(handle) #same handle number as before
browser.switch_to_window(handle)
#again switch frame over if you are working with one
browser.switch_to_frame("Frame")
time.sleep(3)
#doing a second round/tab
elem = browser.find_element_by_xpath("/html/body/div[2]/div[4]/div/a") #href link
time.sleep(2)
elem.send_keys(Keys.CONTROL + Keys.RETURN + "2") #open a 2nd tab again
"""Got it? find the handle, switch over the window then switch the frame"""
对我来说它完美地工作。我可以回答问题...
_webDriver.SwitchTo().Window(_webDriver.WindowHandles.Where(x => x != _webDriver.CurrentWindowHandle).First());
或者使用Last()等。
注意,窗口句柄的顺序可能与浏览器中显示的顺序不同,因此建议在执行导致新标签页打开的命令之前保留当前窗口的历史记录。然后,您可以将存储的窗口句柄与当前集合进行比较,并在列表中切换到新的窗口句柄,其中只应存在一个。
我们无法使用Web Driver / Selenium 2.0创建新的选项卡或处理选项卡。
您可以打开一个新窗口代替。
虽然没有打开新选项卡的API,但你可以创建一个稍微不同名称的WebDriver实例并传递你想要在新选项卡中打开的URL来实现。完成所需的所有操作后,关闭该选项卡并将新的driver设置为NULL,以使其不干扰Webdriver的原始实例。如果需要同时打开两个选项卡,请确保引用适当的WebDriver实例。我曾将它用于Sitecore CMS自动化,并且效果很好。
感谢@Jonathan Azoff提出的好主意!
以下是我在Ruby中的实现:
def open_new_window(url)
a = @driver.execute_script("var d=document,a=d.createElement('a');a.target='_blank';a.href=arguments[0];a.innerHTML='.';d.body.appendChild(a);return a", url)
a.click
@driver.switch_to.window(@driver.window_handles.last)
end
@Test
public void openTab() {
//Open tab 2 using CTRL + t keys.
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");
//Open URL In 2nd tab.
driver.get("http://www.qaautomated.com/p/contact.html");
//Call switchToTab() method to switch to 1st tab
switchToTab();
}
public void switchToTab() {
//Switching between tabs using CTRL + tab keys.
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"\t");
//Switch to current selected tab's content.
driver.switchTo().defaultContent();
}