我正在使用Selenium Automation。在此,當我單擊當前窗口中的鏈接時,會打開一個新窗口。我只想將控件切換到新窗口。但我不能這樣做。 其實新窗口是一個自動生成的窗口。也就是說,鏈接將會動態生成。幫我的朋友...如何在瀏覽器中使用Selenium java切換兩個窗口
回答
在從GitHub上一個getting started with selenium webdriver項目的摘錄,
/**
* Waits for a window to appear, then switches to it.
* @param regex Regex enabled. Url of the window, or title.
* @return
*/
public AutomationTest waitForWindow(String regex) {
Set<String> windows = driver.getWindowHandles();
for (String window : windows) {
try {
driver.switchTo().window(window);
p = Pattern.compile(regex);
m = p.matcher(driver.getCurrentUrl());
if (m.find()) {
attempts = 0;
return switchToWindow(regex);
}
else {
// try for title
m = p.matcher(driver.getTitle());
if (m.find()) {
attempts = 0;
return switchToWindow(regex);
}
}
} catch(NoSuchWindowException e) {
if (attempts <= MAX_ATTEMPTS) {
attempts++;
try {Thread.sleep(1);}catch(Exception x) { x.printStackTrace(); }
return waitForWindow(regex);
} else {
fail("Window with url|title: " + regex + " did not appear after " + MAX_ATTEMPTS + " tries. Exiting.");
}
}
}
// when we reach this point, that means no window exists with that title..
if (attempts == MAX_ATTEMPTS) {
fail("Window with title: " + regex + " did not appear after 5 tries. Exiting.");
return this;
} else {
System.out.println("#waitForWindow() : Window doesn't exist yet. [" + regex + "] Trying again. " + attempts + "/" + MAX_ATTEMPTS);
attempts++;
return waitForWindow(regex);
}
}
/**
* Switch's to a window that is already in existance.
* @param regex Regex enabled. Url of the window, or title.
* @return
*/
public AutomationTest switchToWindow(String regex) {
Set<String> windows = driver.getWindowHandles();
for (String window : windows) {
driver.switchTo().window(window);
System.out.println(String.format("#switchToWindow() : title=%s ; url=%s",
driver.getTitle(),
driver.getCurrentUrl()));
p = Pattern.compile(regex);
m = p.matcher(driver.getTitle());
if (m.find()) return this;
else {
m = p.matcher(driver.getCurrentUrl());
if (m.find()) return this;
}
}
fail("Could not switch to window with title/url: " + regex);
return this;
}
這些是2個自定義功能,以幫助您開始。或者你可以從github上看看這個項目,讓你的硒項目設計得更好,更容易。
這些函數可以切換到或等待(如果它不存在)具有特定標題/ URL的窗口。
是的,這是可能的。首先,您需要將參考保存到當前窗口。
String parentWindow= driver.getWindowHandle();
點擊鏈接後,您需要切換到其他窗口。
List<String> allWindows = driver.getWindowHandles();
for(String curWindow : allWindows){
driver.switchTo().window(curWindow);
}
這是你的新窗口進行操作,最後用
driver.close();
將其關閉,並切換回父窗口
driver.switchTo().window(parentWindow);
它不工作。 – Prasanna
哪部分不工作。顯示您的一些代碼將幫助我們更好地幫助您。切換到新窗口句柄後,將在該窗口上執行所有操作。它應該工作。 – SpartanElite
我記得在IRC中有#selenium這個問題。有人指出了什麼是嘗試嘗試。我編輯了代碼,以便清楚地說明,但是您必須確保在循環顯示窗口句柄的同時,您並不真正切換到當前窗口。 – aimbire
我終於找到了答案, 我用下面的方法切換到新窗口,
public String switchwindow(String object, String data){
try {
String winHandleBefore = driver.getWindowHandle();
for(String winHandle : driver.getWindowHandles()){
driver.switchTo().window(winHandle);
}
}catch(Exception e){
return Constants.KEYWORD_FAIL+ "Unable to Switch Window" + e.getMessage();
}
return Constants.KEYWORD_PASS;
}
要移動到父窗口,我用下面的代碼,
public String switchwindowback(String object, String data){
try {
String winHandleBefore = driver.getWindowHandle();
driver.close();
//Switch back to original browser (first window)
driver.switchTo().window(winHandleBefore);
//continue with original browser (first window)
}catch(Exception e){
return Constants.KEYWORD_FAIL+ "Unable to Switch to main window" + e.getMessage();
}
return Constants.KEYWORD_PASS;
}
To switch between windows we have method. driver.switchTo().window("window name")To get the different windows handle, we have method.<b>driver.getWindowHandles()
Example:
File file = new File("G:\\Selenium\\All_Jars\\chromedriver.exe");
System.setProperty("webdriver.chrome.driver",file.getAbsolutePath());
driver = new ChromeDriver();
//Maximize the window
driver.manage().window().maximize();
driver.get("http://www.rediff.com/");
//Get all window handles
Set<String> allHandles = driver.getWindowHandles();
//count the handles Here count is=2
System.out.println("Count of windows:"+allHandles.size());
//Get current handle or default handle
String currentWindowHandle = allHandles.iterator().next();
System.out.println("currentWindow Handle"+currentWindowHandle);
//Remove first/default Handle
allHandles.remove(allHandles.iterator().next());
//get the last Window Handle
String lastHandle = allHandles.iterator().next();
System.out.println("last window handle"+lastHandle);
//switch to second/last window, because we know there are only two windows 1-parent window 2-other window(ad window)
driver.switchTo().window(lastHandle);
System.out.println(driver.getTitle());
driver.findElement(By.tagName("body")).click();
Set <String> set = driver.getWindowHandles();
Iterator<String> it = set.iterator();
String parentWindowId = it.next();
String childWindowId = it.next();
System.out.println(set);
driver.switchTo().window(childWindowId);
// fetch all windows before clicking on new window link.
Set<String> windowHandles = driver.getWindowHandles();
// Click on link to open new window
driver.findElement(By.tagName("a")).click(); // link to open new window
Set<String> updatedWindowHandles = driver.getWindowHandles();
updatedWindowHandles.removeAll(windowHandles);
for (String window: updatedWindowHandles) {
driver.switchTo().window(window);
}
//to get the current/parent window
String parentWindowContact = driver.getWindowHandle();
//to switch to the new window from current/parent window
Set<String> handleswindow =driver.getWindowHandles();
for(String windowHandle : handleswindow)
{
driver .switch To().window(windowHandle);
}
//to close the new window
driver.close();
//to switch back to the parent window
driver.switchTo().window(parentWindowContact);
o switch back to the parent window
driver.switchTo().window(parentWindowContact);
- 1. 使用Selenium WebDriver在兩個瀏覽器窗口之間切換
- 2. 無法使用Selenium WebDriver在兩個瀏覽器窗口之間切換
- 3. 使用Selenium Webdriver在瀏覽器窗口之間切換
- 4. 在java中使用selenium切換窗口
- 5. 如何在兩個瀏覽器之間切換使用java的selenium webdriver
- 6. Selenium 2窗口切換:Java
- 7. 使用Selenium切換到Firefox瀏覽器
- 8. 如何使用Selenium Java在兩個瀏覽器標籤頁/窗口之間獲得活動窗口標題
- 9. Selenium-IDE測試不能切換瀏覽器窗口
- 10. 如何使用selenium webdriver java切換到另一個窗口?
- 11. Selenium web driver | java |無法在Firefox瀏覽器窗口之間切換
- 12. 無法切換到主窗口內的瀏覽器窗口(Selenium Webdriver)
- 13. 如何在使用Java的Selenium WebDriver中關閉子瀏覽器窗口
- 14. 如何在php中使用selenium中的兩個瀏覽器
- 15. 在Emacs的ECB中,如何使用鍵盤切換到瀏覽器窗口?
- 16. 如何使用java處理selenium中的瀏覽器通知彈出窗口?
- 17. 在瀏覽器中的兩個窗口/選項卡之間切換
- 18. 如何使用selenium webDriver最小化IE瀏覽器的窗口?
- 19. 讓selenium java在瀏覽器窗口中間點擊
- 20. 如何使整個瀏覽器窗口
- 21. 使用Lambda表達式在Selenium中使用Java切換窗口使用Java
- 22. 兩個瀏覽器在Selenium中打開
- 23. Selenium關閉瀏覽器彈出窗口
- 24. 如何使用Selenium WebDriver for python在瀏覽器上打開一個新窗口?
- 25. 切換兩個主窗口
- 26. Selenium,FluentAutomation&NUnit - 如何切換每個TestCase的瀏覽器?
- 27. Selenium:無法切換窗口
- 28. 如何在一個瀏覽器窗口中在Grid中運行Selenium測試?
- 29. 如何在python中使用Selenium關閉瀏覽器彈出窗口?
- 30. 如何使用硒webdriver c切換焦點到瀏覽器彈出窗口#
它不工作,先生。 – Prasanna
確保您將「AutomationTest」變量更改爲「void」。這種方法已被證明並且有效。它在你身邊,它不工作。如果你想要一個工作副本,然後下載[這是從這個項目](https://github.com/ddavison/getting-started-with-selenium/archive/master.zip),並將其導入到Eclipse到看到。 – sircapsalot
好的,先生。謝謝..! – Prasanna