2016-04-05 5 views
0

getDriver()。getWindowHandles()を使用してウィンドウを切り替えることに関連する解答が見つかりました。Javaのselenium webdriverを使用してページタイトルでウィンドウ間を切り替えることはできますか?

しかし、Javaを使用してセレンのWebdriverでページタイトルを使用してウィンドウを切り替える方法があるかどうかを知りたいと思います。

注:私はSelenium Frameworkの初心者です。

+0

タイトルに基づいてウィンドウを切り替える唯一の方法は、タイトルを確認して、それが一致するとそのウィンドウに固執することです。 –

答えて

0

はいできます。

String your_title = "This is the Title"; 
String currentWindow = driver.getWindowHandle(); //will keep current window to switch back 
for(String winHandle : driver.getWindowHandles()){ 
    if (driver.switchTo().window(winHandle).getTitle().equals(your_title)) { 
    //This is the one you're looking for 
    break; 
    } 
    else { 
     driver.switchTo().window(currentWindow); 
    } 
} 
0

こんにちは、あなたは、これはあなたが探しているものができます

driver.get("http://www.seleniumhq.com"); 
// for understanding point please open a new tab 
driver.findElement(By.tagName("body")).sendKeys(Keys.CONTROL + "t"); 
// note new tab title will be blank as we have not opened anything in it. 

// Store the current window handle- the parent one 
String winHandleBefore = driver.getWindowHandle(); 
// store all the windows 
Set<String> handle= driver.getWindowHandles(); 
// iterate over window handles 
for(String mywindows : handle){ 
// store window title 
String myTitle = driver.switchTo().window(mywindows).getTitle(); 
// now apply the condition - moving to the window with blank title 
    if(myTitle.equals("")){ 
    // perform some action - as here m openning a new url 
    driver.get("http://docs.seleniumhq.org/download/"); 
}else{ 
    driver.switchTo().window(winHandleBefore); 
} 
} 

希望を下回るようにしてみてください。

0

あなたは(すなわち、ウィンドウのトップレベル<title>タグの正確な内容)にフォーカスを切り替えたいウィンドウの正確なタイトルを持っている場合は、あなたは、単に次の操作を実行できます。

driver.switchTo().window("Whatever the title is, as a String"); 

フォーカスを切り替えるウィンドウのタイトルの一部しかない場合は、他の答えと同様に、使用可能なウィンドウをハンドルで反復処理する必要があります。

+0

switchTo()メソッドdoesnot引数を許してください。 – Chandni

+0

確かに、私はそれに応じてコードの部分を修正しました。 –

関連する問題