Selenium 2-名前/IDのないフレームにフォーカスを切り替える
-
30-09-2019 - |
質問
だから今、フレームに名前やIDがないときに、Selenium2のフレームにフォーカスを切り替える方法を見つけようとしていますか?名前付きフレームの場合私はします:
driver.SwitchTo().Frame(FrameName);
しかし、名前は何ですか?誰かがこれを経験しましたか?
解決
driver.switchto.frame()is フレーム名または整数を受け入れるために過負荷. 。このINTは、利用可能なフレームの0ベースのインデックスです。最初のフレームは0、2番目の1つなどです。
このHTMLページに対してJava BindingとFirefoxを使用して、非常に簡単なテストを実行しました。
<html>
<frameset rows="50%,50%">
<frame src="frame_a.htm" />
<frame src="frame_b.htm" />
</frameset>
</html>
driver.switchto()。フレーム(0)を使用することができます。 Frame AおよびDriver.switchto()。フレーム(1)を参照してください。フレームにアクセスするにはb。
他のヒント
フレームのインデックスを使用できます。フレームの名前とIDがないので、driver.switchto()。フレーム(int frameindex)
セレニウムでは、フレームの相対位置が文字列「相対= up」でselectframeコマンドを使用してフレームを上に移動できることを知っている場合、セレニウムで selenium.SelectFrame("relative=up");
または、トップフレームにジャンプするには、「Recarty = Top」を使用します
iframe-nameの代わりにiframeのIDを与えることができます。
以下の例をご覧ください。それは私のために働いていました。
この例では、私のページで1つのiframeに切り替えて、そのiframe(「worksheet0」」の要素をクリックしています。
コードを使用してください:
driver.switchTo().frame("topframe");
WebElement worksheet0 = driver.findElement(By.xpath("//*@id='reportSelect:Worksheet_lbl']")); worksheet0.click();
iframeのhtmlof:
< iframe id="topframe" height="83px" frameborder="0" width="100%" scrolling="NO" '1331808552380'"="" +="" src="initialize.do?init=header&cacheBuster=" name="topframe" marginheight="0" marginwidth="0">
インデックスを使用することに加えて(他の回答が示唆するように)、 C# Tagnameでiframeを選択できます。私の例では、ページに1つのiframeが1つだけあると仮定しています。
try
{
var iFrameElement = Driver.FindElementByTagName("iFrame");
var driver = Driver.SwitchTo().Frame(this.iFrameElement);
var element = driver.FindElement(selector);
// do what you need with the element
}
finally
{
// don't forget to switch back to the DefaultContent
Driver.SwitchTo().DefaultContent();
}
注:driver.switchto()。defaultcontent()を呼び出す前に、iwebelement .textまたは.clickから情報を取得する必要があります。
これらのエクステンションメソッドを作成して支援しました
public static IWebDriver SwitchToIFrame(this RemoteWebDriver driver)
{
// http://computerrecipes.wordpress.com/2012/08/23/selenium-webdriver-interact-with-an-element-inside-an-iframe/
// http://stackoverflow.com/questions/3549584/selenium-2-switching-focus-to-a-frame-that-has-no-name-id
var iFrameElement = driver.FindElementByTagName("iFrame");
return driver.SwitchTo().Frame(iFrameElement);
}
public static void SwitchOutOfIFrame(this IWebDriver driver)
{
driver.SwitchTo().DefaultContent();
}
拡張機能の使用例:
public void ClickPrintButton()
{
var iFrameDriver = Browser.Driver.SwitchToIFrame();
try
{
iFrameDriver.FindElement(By.Id("saveButton")).Click();
}
finally
{
Browser.Driver.SwitchOutOfIFrame();
}
}