[英]How to work on opened page after opening it on new tab using selenium webdriver?
I am able to switch on another tab at same browser but after opening that page, on another tab I am not able to perform the operation. 我可以在同一浏览器上打开另一个选项卡,但是打开该页面后,不能在另一个选项卡上执行该操作。
Current scenario held on: 目前的情况是:
But after switch on that window I want to give credential again on login page, but don't want to close the first opened tab on which account is loggedIn. 但是在打开该窗口后,我想在登录页面上再次提供凭据,但是不想关闭第一个打开的登录帐户的标签。
I am doing it in framework so please check the below code and according to this integrate your code and help me. 我正在框架中进行操作,因此请检查以下代码,并根据此代码集成您的代码并为我提供帮助。
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(10, TimeUnit.SECONDS);
if (operation.equalsIgnoreCase("Click")) {
temp.click();
}
if (operation.equalsIgnoreCase("newTab")) {
System.out.println("newTab" + temp);
Actions newTab = new Actions(driver);
newTab.keyDown(Keys.CONTROL).click(temp).keyUp(Keys.CONTROL).build().perform();
newTab.sendKeys(Keys.chord(Keys.CONTROL,Keys.TAB)).perform();
}
if (operation.equalsIgnoreCase("Verify")) {
System.out.println("Verify--->" + temp);
temp.isDisplayed();
}
testCaseStep = true;
} catch (Exception e) {
System.out.println("Exception occurred operateWebDriver"
+ e.getMessage());
//Taking snapshot when test case fail.
System.out.println("Taking snapshot");
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("E:\\workspace for selenium\\AutomationFramework\\AutomationFramework\\Screenshots\\screenshot.jpg"));
}
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));
System.out.println("name temp ----->" + temp);
}
return temp;
}
}
Now I am able to work on opened URL after opening it on another tab. 现在,在另一个选项卡上打开URL之后,我就可以对其进行处理。 Here is the code which is working from my end.
这是从头开始起作用的代码。
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(10, TimeUnit.SECONDS);
if (operation.equalsIgnoreCase("Click")) {
temp.click();
}
if (operation.equalsIgnoreCase("newTab")) {
System.out.println("newTab" + temp);
// Open link in new tab.
Actions newTab = new Actions(driver);
newTab.keyDown(Keys.CONTROL).click(temp).keyUp(Keys.CONTROL).build().perform();
//After Opening link in new tab display that window.
newTab.sendKeys(Keys.chord(Keys.CONTROL,Keys.TAB)).perform();
// Get the number of tab opened in browser.
ArrayList<String> newTab1 = new ArrayList<String>(driver.getWindowHandles());
// change focus to new tab
driver.switchTo().window(newTab1.get(0));
// Now perform the operation here.
if (operation.equalsIgnoreCase("SendKey")) {
temp.sendKeys(value);
}
Thread.sleep(1000);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
if (operation.equalsIgnoreCase("Click")) {
temp.click();
}
}
if (operation.equalsIgnoreCase("Verify")) {
System.out.println("Verify--->" + temp);
temp.isDisplayed();
}
testCaseStep = true;
} catch (Exception e) {
System.out.println("Exception occurred operateWebDriver"
+ e.getMessage());
//Taking snapshot when test case fail.
System.out.println("Taking snapshot");
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("E:\\workspace for selenium\\AutomationFramework\\AutomationFramework\\Screenshots\\screenshot.jpg"));
}
return testCaseStep;
}
which browser are you working on? 您正在使用哪个浏览器? firefox doesn't work on tab.
Firefox在选项卡上不起作用。 try using chrome.. store current window handles and click the desired link to open new tab
尝试使用chrome ..存储当前的窗口句柄,然后单击所需的链接以打开新标签
//current window - before opening link on new tab
String winHandleBefore = driver.getWindowHandle();
//write code to open new link
tabs2 = new ArrayList<String> (driver.getWindowHandles());
for( int k= 0 ; k< tabs2.size(); k++ ){
for(String winHandle : driver.getWindowHandles()){
driver.switchTo().window(tabs2.get(k));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.