![](/img/trans.png)
[英]How to open a new tab in the same browser by using Selenium WebDriver with Java?
[英]How to open url on another tab at same browser using selenium webdriver?
我想通過單擊添加帳戶按鈕在新選項卡上打開URL。 我正在使用框架,也找到了一些解決方案,但不了解如何應用它。
以下是我找到元素並返回以執行操作的代碼。 誰能通過此代碼讓我知道在新選項卡中集成用於打開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;
}
}
您可以將此條件添加到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
}
例如,如果您登陸http://stackoverflow.com,並且想要在新標簽頁中打開“問題”按鈕,該標簽與您要執行的操作類似,則可以調用如下方法:
//operateWebDriver(String operation, String Locator, String value, String objectName)
operateWebDriver("newTab", "xpath", "", "//*[@id='nav-questions']");
有關使用硒打開新標簽的更多信息:
如何使用Selenium WebDriver打開新標簽並啟動鏈接?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.