Java 在Selenium WebDriver中打开新选项卡后,如何使其成为浏览器中的可见/活动选项卡?
我正在使用以下WebDriver Java代码打开一个新选项卡(当然有更好的方法可以做到这一点):Java 在Selenium WebDriver中打开新选项卡后,如何使其成为浏览器中的可见/活动选项卡?,java,selenium,selenium-webdriver,tabs,webdriver,Java,Selenium,Selenium Webdriver,Tabs,Webdriver,我正在使用以下WebDriver Java代码打开一个新选项卡(当然有更好的方法可以做到这一点): WebElement link=driver.findElement(By.id(“主页按钮”); Actions newTab=新操作(驱动程序); newTab.keyDown(Keys.CONTROL).单击(link).keydup(Keys.CONTROL).build().perform(); ArrayList openTabs=新的ArrayList(driver.getWindo
WebElement link=driver.findElement(By.id(“主页按钮”);
Actions newTab=新操作(驱动程序);
newTab.keyDown(Keys.CONTROL).单击(link).keydup(Keys.CONTROL).build().perform();
ArrayList openTabs=新的ArrayList(driver.getWindowHandles());
driver.switchTo().window(openTabs.get(1));
驱动程序。导航()。到(“http://google.com");
它工作得非常好,我所做的任何进一步的操作都会应用到新的选项卡上,但它在我的屏幕上不可见,我仍然可以看到打开的第一个选项卡。有没有办法更改我的窗口中可见的选项卡
感谢您切换选项卡,请使用
newTab.sendKeys(Keys.chord(Keys.CONTROL,Keys.TAB)).perform();
要切换选项卡,请使用
newTab.sendKeys(Keys.chord(Keys.CONTROL,Keys.TAB)).perform();
要切换选项卡,请使用
newTab.sendKeys(Keys.chord(Keys.CONTROL,Keys.TAB)).perform();
要切换选项卡,请使用
newTab.sendKeys(Keys.chord(Keys.CONTROL,Keys.TAB)).perform();
实际上,在选项卡之间切换是可以的。但是在它们之间切换控件是不可能的,因为即使您打开一个新选项卡,它仍然在主窗口中,并且除非您关闭原始选项卡,否则控件将不会切换到新选项卡 因此,您可以在新窗口中打开链接,并获取新打开窗口的窗口句柄。然后,您可以在两个窗口之间切换(连同切换手柄)并执行必要的操作。 下面是一个使用“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
}
}
实际上,在选项卡之间切换是可以的。但是在它们之间切换控件是不可能的,因为即使您打开一个新选项卡,它仍然在主窗口中,并且除非您关闭原始选项卡,否则控件将不会切换到新选项卡 因此,您可以在新窗口中打开链接,并获取新打开窗口的窗口句柄。然后,您可以在两个窗口之间切换(连同切换手柄)并执行必要的操作。 下面是一个使用“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
}
}
实际上,在选项卡之间切换是可以的。但是在它们之间切换控件是不可能的,因为即使您打开一个新选项卡,它仍然在主窗口中,并且除非您关闭原始选项卡,否则控件将不会切换到新选项卡 因此,您可以在新窗口中打开链接,并获取新打开窗口的窗口句柄。然后,您可以在两个窗口之间切换(连同切换手柄)并执行必要的操作。 下面是一个使用“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
}
}
实际上,在选项卡之间切换是可以的。但是在它们之间切换控件是不可能的,因为即使您打开一个新选项卡,它仍然在主窗口中,并且除非您关闭原始选项卡,否则控件将不会切换到新选项卡 因此,您可以在新窗口中打开链接,并获取新打开窗口的窗口句柄。然后,您可以在两个窗口之间切换(连同切换手柄)并执行必要的操作。 下面是一个使用“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
}
}
我尝试了上面的方法,不幸的是,我仍然可以看到我原来的选项卡。是的,因为这只是将您切换到一个新选项卡。。您需要删除上一个选项卡还是什么?我希望切换到的选项卡是我在窗口中实际可以看到的选项卡。好的,用以下代码替换上面的代码:
newTab.sendKeys(Keys.chord(Keys.CONTROL,Keys.F4)).perform()代码>在单击并打开新选项卡之前,它将关闭当前打开的选项卡(我认为是原始选项卡)。@Subh F4将自行关闭选项卡我尝试了上述操作,但不幸的是,我仍然可以看到我的原始选项卡。是的,因为这会将您切换到新选项卡。。您需要删除上一个选项卡还是什么?我希望切换到的选项卡是我在窗口中实际可以看到的选项卡。好的,用以下代码替换上面的代码:newTab.sendKeys(Keys.chord(Keys.CONTROL,Keys.F4)).perform()代码>在单击并打开新选项卡之前,它将关闭当前打开的选项卡(我认为是原始选项卡)。@Subh F4将自行关闭选项卡我尝试了上述操作,但不幸的是,我仍然可以看到我的原始选项卡。是的,因为这会将您切换到新选项卡。。您需要删除上一个选项卡还是什么?我希望切换到的选项卡是我在窗口中实际可以看到的选项卡。好的,用以下代码替换上面的代码:newTab.sendKeys(Keys.chord(Keys.CONTROL,Keys.F4)).perform()代码>在单击并打开新选项卡之前,它将关闭当前打开的选项卡(我认为是原始选项卡)。@Subh F4将自行关闭选项卡我尝试了上述操作,但不幸的是,我仍然可以看到我的原始选项卡。是的,因为这会将您切换到新选项卡。。您需要删除上一个选项卡还是什么?我希望切换到的选项卡是我在窗口中实际可以看到的选项卡。好的,用以下代码替换上面的代码:newTab.sendKeys(Keys.chord(Keys.CONTROL,Keys.F4)).perform()代码>在单击并打开新选项卡之前,它将关闭当前打开的选项卡,我认为这是原始选项卡。@Subh F4将关闭该选项卡itself@Ash:如果您不明白上面代码中发生了什么,请告诉我。大约一年后-切换控件2当前活动的选项卡已完成此操作:driver.SwitchTo().DefaultContent()。另外,我通过发送:Ctrl+(制表符编号)在制表符之间切换。这样你可以循环或者直接进入标签页,真是妙不可言!谢谢