如何通过Selenium和Webdriver打开多个标签页并切换?
How to open multiple tabs and switch between through Selenium and Webdriver?
我用谷歌搜索了这段代码,但没有找到正确的代码。我有一个场景,我们的应用程序有 5 个模块,我想在每个选项卡上打开它,因为我需要在它们之间切换多次,请帮助
这不起作用:
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");
使用 getWindowHandles 你可以切换 url 多个 time.Here 首先我切换 url 并识别标签,每个标签应该是唯一的 url 我们可以通过 url。
for (String window : driver.getWindowHandles()) {
driver.switchTo().window(window);
if (driver.getCurrentUrl().contains("google.com")) {
//Your operation
}
if (driver.getCurrentUrl().contains("yahoo.com")) {
}
//Your operation
}
例如你在两个选项卡中打开 google 和 yahoo 然后你可以切换选项卡并识别选项卡并在那里进行任何操作。
希望对您有所帮助
以下是通过 Selenium Webdriver 打开多个选项卡并在它们之间切换的示例:
代码块:
import java.util.Set;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class WINDOW_HANDLE_ITERATE_Firefox
{
public static void main(String[] args) throws Exception
{
System.setProperty("webdriver.gecko.driver", "C:\Utility\BrowserDrivers\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
String parent_window = driver.getWindowHandle();
System.out.println("Parent Window Handle is: "+driver.getWindowHandle());
System.out.println("Page Title is: "+driver.getTitle());
((JavascriptExecutor) driver).executeScript("window.open('http://facebook.com/');");
new WebDriverWait(driver,10).until(ExpectedConditions.numberOfWindowsToBe(2));
Set<String> allWindows_1 = driver.getWindowHandles();
System.out.println("Total Windows: "+allWindows_1.size());
for(String hand1:allWindows_1)
if(!parent_window.equals(hand1))
{
driver.switchTo().window(hand1);
new WebDriverWait(driver,10).until(ExpectedConditions.titleContains("Facebook"));
String first_child_window = driver.getWindowHandle();
System.out.println("First Child Window Handle is: "+first_child_window);
System.out.println("First Child Window Page Title is: "+driver.getTitle());
driver.close();
}
driver.switchTo().window(parent_window);
System.out.println("Current Window Handle is : "+driver.getWindowHandle()+ " which is same as "+parent_window +", which is the parent window handle" );
driver.quit();
}
}
控制台输出:
INFO: Detected dialect: W3C
Parent Window Handle is: 6442450945
Page Title is: Google
Total Windows: 2
First Child Window Handle is: 6442450949
First Child Window Page Title is: Facebook – log in or sign up
Current Window Handle is : 6442450945 which is same as 6442450945, which is the parent window handle
你可以试试这个,
要使用 JavascriptExecutor 打开新标签页,
((JavascriptExecutor) driver).executeScript("window.open()");
ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles());
driver.switchTo().window(tabs.get(1));
将根据索引在选项卡上进行控制:
driver.switchTo().window(tabs.get(1));
主选项卡上的驱动程序控件:
driver.switchTo().window(tabs.get(0));
我用谷歌搜索了这段代码,但没有找到正确的代码。我有一个场景,我们的应用程序有 5 个模块,我想在每个选项卡上打开它,因为我需要在它们之间切换多次,请帮助
这不起作用:
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");
使用 getWindowHandles 你可以切换 url 多个 time.Here 首先我切换 url 并识别标签,每个标签应该是唯一的 url 我们可以通过 url。
for (String window : driver.getWindowHandles()) {
driver.switchTo().window(window);
if (driver.getCurrentUrl().contains("google.com")) {
//Your operation
}
if (driver.getCurrentUrl().contains("yahoo.com")) {
}
//Your operation
}
例如你在两个选项卡中打开 google 和 yahoo 然后你可以切换选项卡并识别选项卡并在那里进行任何操作。
希望对您有所帮助
以下是通过 Selenium Webdriver 打开多个选项卡并在它们之间切换的示例:
代码块:
import java.util.Set; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; public class WINDOW_HANDLE_ITERATE_Firefox { public static void main(String[] args) throws Exception { System.setProperty("webdriver.gecko.driver", "C:\Utility\BrowserDrivers\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("http://www.google.com"); String parent_window = driver.getWindowHandle(); System.out.println("Parent Window Handle is: "+driver.getWindowHandle()); System.out.println("Page Title is: "+driver.getTitle()); ((JavascriptExecutor) driver).executeScript("window.open('http://facebook.com/');"); new WebDriverWait(driver,10).until(ExpectedConditions.numberOfWindowsToBe(2)); Set<String> allWindows_1 = driver.getWindowHandles(); System.out.println("Total Windows: "+allWindows_1.size()); for(String hand1:allWindows_1) if(!parent_window.equals(hand1)) { driver.switchTo().window(hand1); new WebDriverWait(driver,10).until(ExpectedConditions.titleContains("Facebook")); String first_child_window = driver.getWindowHandle(); System.out.println("First Child Window Handle is: "+first_child_window); System.out.println("First Child Window Page Title is: "+driver.getTitle()); driver.close(); } driver.switchTo().window(parent_window); System.out.println("Current Window Handle is : "+driver.getWindowHandle()+ " which is same as "+parent_window +", which is the parent window handle" ); driver.quit(); } }
控制台输出:
INFO: Detected dialect: W3C Parent Window Handle is: 6442450945 Page Title is: Google Total Windows: 2 First Child Window Handle is: 6442450949 First Child Window Page Title is: Facebook – log in or sign up Current Window Handle is : 6442450945 which is same as 6442450945, which is the parent window handle
你可以试试这个,
要使用 JavascriptExecutor 打开新标签页,
((JavascriptExecutor) driver).executeScript("window.open()");
ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles());
driver.switchTo().window(tabs.get(1));
将根据索引在选项卡上进行控制:
driver.switchTo().window(tabs.get(1));
主选项卡上的驱动程序控件:
driver.switchTo().window(tabs.get(0));