我需要在多个firefox选项卡上运行不同的任务。每个选项卡将在应用程序的开头加载不同的URL。一个进程将在选项卡上运行,转到下一个,运行下一个选项卡,转到下一个选项卡等。我的问题是当我返回到在应用程序启动时打开的选项卡时,我必须运行driver.get(url)再次为了再次控制该标签。有没有办法控制选项卡而不必再次运行driver.get(url)来控制页面?
我的代码示例:
main(){
int ix = 1;
while(ix <= numberOfTabstoRun){
driver.get(url[ix]);
if(ix != numberOfTabstoRun){
openNewTab(driver);
}
ix++;
}
/**THIS WONT WORK - can't control next tab**/
while(true){
//run process on open tab
nextTab(driver);
}
/**I am forced to do this**/
int x = 1;
while(true){
driver.get(url[x]);
//run process on open tab
nextTab(driver);
x++;
}
}
private void openNewTab(WebDriver driver){
new Actions(driver).sendKeys(Keys.chord(Keys.CONTROL, "t")).perform();
}
private void nextTab(WebDriver driver){
new Actions(driver).sendKeys(Keys.chord(Keys.CONTROL, Keys.TAB)).perform();
}
答案 0 :(得分:3)
发送ctrl+tab
后,您需要将驱动程序切换到新目标。
private void nextTab(WebDriver driver){
new Actions(driver).sendKeys(Keys.chord(Keys.CONTROL, Keys.TAB)).perform();
driver.switchTo().defaultContent();
}
http://design-interviews.blogspot.com/2014/11/switching-between-tabs-in-same-browser-window.html
的更多详情