[英]How to open url on another tab at same browser using selenium webdriver?
I want to open a URL on new tab by clicking on button for add account. 我想通过单击添加帐户按钮在新选项卡上打开URL。 I am working with framework, also I've found some solution but don't understand how to apply it.
我正在使用框架,也找到了一些解决方案,但不了解如何应用它。
Below are the code where I've find the element and return it for performing the operations. 以下是我找到元素并返回以执行操作的代码。 Can any one let me know with integrate the code for opening the url in new tab, using this code?
谁能通过此代码让我知道在新选项卡中集成用于打开url的代码?
private boolean operateWebDriver(String operation, String Locator,
String value, String objectName) throws Exception {
boolean testCaseStep = false;
try {
System.out.println("Operation execution in progress");
WebElement temp = getElement(Locator, objectName);
if (operation.equalsIgnoreCase("SendKey")) {
temp.sendKeys(value);
}
Thread.sleep(1000);
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
if (operation.equalsIgnoreCase("Click")) {
temp.click();
//try to open account on another tab.
String myWindowHandel= driver.getWindowHandle();
driver.switchTo().window(myWindowHandel);
}
if (operation.equalsIgnoreCase("Verify")) {
System.out.println("Verify--->" + temp);
temp.isDisplayed();
}
testCaseStep = true;
} catch (Exception e) {
System.out.println("Exception occurred operateWebDriver"
+ e.getMessage());
// Take screenshot if any testcase is not working.
System.out.println("Taking Screen Shot");
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("D:\\softs\\eclipse-jee-kepler-SR1-win32\\Workspace\\AutomationFramework\\Screenshot\\screenshot.jpeg"));
}
return testCaseStep;
}
public WebElement getElement(String locator, String objectName)
throws Exception {
WebElement temp = null;
System.out.println("Locator-->" + locator);
if (locator.equalsIgnoreCase("id")) {
temp = driver.findElement(By.id(objectName));
} else if (locator.equalsIgnoreCase("xpath")) {
temp = driver.findElement(By.xpath(objectName));
System.out.println("xpath temp ----->" + temp);
} else if (locator.equalsIgnoreCase("name")) {
temp = driver.findElement(By.name(objectName));
}
return temp;
}
}
You can add this if condition into your operateWebDriver() 您可以将此条件添加到operateWebDriver()中
if (operation.equalsIgnoreCase("newTab")) {
System.out.println("newTab" + temp);
Actions newTab = new Actions(driver);
newTab.keyDown(Keys.COMMAND).click(temp).keyUp(Keys.COMMAND).build().perform();
// Do your switch windows and other stuff you plan here after opening the new tab
}
For example if you landed on http://stackoverflow.com and you want to open Questions button in a new tab which is similar to what you want to do, you can call the method like this: 例如,如果您登陆http://stackoverflow.com,并且想要在新标签页中打开“问题”按钮,该标签与您要执行的操作类似,则可以调用如下方法:
//operateWebDriver(String operation, String Locator, String value, String objectName)
operateWebDriver("newTab", "xpath", "", "//*[@id='nav-questions']");
For more info on opening a new tab using selenium: 有关使用硒打开新标签的更多信息:
How to open a new tab using Selenium WebDriver and start the link? 如何使用Selenium WebDriver打开新标签并启动链接?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.