我正在使用以下WebDriver Java代码打开一个新标签(授予可能更好的方法):
WebElement link = driver.findElement(By.id("home_button"));
Actions newTab = new Actions(driver);
newTab.keyDown(Keys.CONTROL).click(link).keyUp(Keys.CONTROL).build().perform();
ArrayList<String> openTabs = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(openTabs.get(1));
driver.navigate().to("http://google.com");
它运行得非常好,我所做的任何进一步操作都应用于新标签,但在屏幕上看不到,我仍然可以看到第一个打开的标签。有没有办法改变窗口中可见的标签?
由于
答案 0 :(得分:2)
要切换标签,请使用
newTab.sendKeys(Keys.chord(Keys.CONTROL,Keys.TAB)).perform();
答案 1 :(得分:1)
实际上,在标签之间切换很好。但是在它们之间切换控制是不可能的因为即使你打开一个新选项卡它仍然在主窗口内,除非你关闭原始选项卡,否则控件不会转换为新选项卡。
因此,您可以做的是在新窗口中打开链接,并获取新打开窗口的窗口句柄。然后,您可以在两个窗口(以及切换手柄)之间切换并执行必要的操作。
以下是使用“google.com”网站的示例。请执行代码并检查流程。你会明白我的意思:
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
public class Testing_window_switching{
public static void main(String args[]) throws InterruptedException{
WebDriver driver = new FirefoxDriver(); //Opening firefox instance
driver.manage().window().maximize(); //maximizing window
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); //Giving implicit timeout of 20 seconds
driver.get("http://google.co.in/");
driver.findElement(By.id("gbqfq")).sendKeys("stackoverflow");
driver.findElement(By.xpath("//ul[@role='listbox']//div[.='stack overflow']")).click();
String parentWindow = driver.getWindowHandle();//Getting parent window handle
System.out.println("Parent Handle is: "+parentWindow);
Actions newTab= new Actions(driver);
WebElement link = driver.findElement(By.xpath("//div[@class='srg']//a[.='Stack overflow - Wikipedia, the free encyclopedia']"));
//Opening the link having text 'Stack overflow - Wikipedia, the free encyclopedia']' in new window
newTab.contextClick(link).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).build().perform();
String childWindow = null;
for(String childs: driver.getWindowHandles()){
if(!childs.equals(parentWindow)){
childWindow = childs;
break;
}
}
System.out.println("Child handle is: "+ childWindow);
driver.switchTo().window(childWindow); //To switch handle to child window
driver.findElement(By.xpath("//a[.='Stack overflow (disambiguation)']")).click();
driver.navigate().back();
newTab.sendKeys(Keys.chord(Keys.ALT,Keys.TAB)).perform(); //To switch to main window
driver.switchTo().window(parentWindow); //To switch current handle to parent
System.out.println("Switched to parent window");
driver.findElement(By.xpath("//div[@class='srg']//a[.='Blog – Stack Exchange']")).click();// Click on the link 'Blog – Stack Exchange' in parent window
newTab.sendKeys(Keys.chord(Keys.ALT,Keys.TAB)).perform(); //To switch to child window.
driver.switchTo().window(childWindow); //To switch current handle to child
System.out.println("Switched to child window");
driver.findElement(By.xpath("//li[@id='n-mainpage-description']/a")).click();//Clicking on the link 'Main Page' in the Child window
Thread.sleep(10000);//Sleep time of 10 seconds, not necessary but just to give some time before quitting
driver.quit(); //Closing all instances of browser
}
}