![](/img/trans.png)
[英]After opening a new tab in Selenium WebDriver, how to make it the visible/active tab in my browser?
[英]How to work on opened page after opening it on new tab using selenium webdriver?
我可以在同一瀏覽器上打開另一個選項卡,但是打開該頁面后,不能在另一個選項卡上執行該操作。
目前的情況是:
但是在打開該窗口后,我想在登錄頁面上再次提供憑據,但是不想關閉第一個打開的登錄帳戶的標簽。
我正在框架中進行操作,因此請檢查以下代碼,並根據此代碼集成您的代碼並為我提供幫助。
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;
}
}
現在,在另一個選項卡上打開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(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;
}
您正在使用哪個瀏覽器? Firefox在選項卡上不起作用。 嘗試使用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.