Java 无法在 selenium webdriver 中选择 Iframe

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25721263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 01:00:33  来源:igfitidea点击:

Can't select an Iframe in selenium webdriver

javaiframeseleniumselenium-webdriver

提问by Ali Hamadi

I am trying to select an Iframeby a class name but it's not working , I am trying with tagNameit works but then when I tried to type within the element in the IframeI couldn't, could you please help me here is my code:

我正在尝试Iframe按类名选择一个,但它不起作用,我正在尝试使用tagName它,但是当我尝试在元素中输入时,Iframe我不能,请您帮我看看这是我的代码:

webDriver driver.switchTo().frame( driver.findElement( By.className( "cke_wysiwyg_frame cke_reset" ) ) );
driver.findElement( By.xpath( "//body[contains(text(),'type here')]" ) ).sendKeys( "Testing" );

And here is the HTML in my webpage:

这是我网页中的 HTML:

<div id="cke_534_contents" class="cke_contents cke_reset" role="presentation" style="height: 75px;">
   <span id="cke_586" class="cke_voice_label">Press ALT 0 for help</span>
   <iframe class="cke_wysiwyg_frame cke_reset" frameborder="0" src="" style="width: 100%; height: 100%;" aria-describedby="cke_586" tabindex="0" allowtransparency="true">
      <!DOCTYPE html>
      <html lang="en-gb" dir="ltr">
         <head>
         <body class="cke_editable cke_editable_themed cke_contents_ltr cke_show_borders" contenteditable="true" spellcheck="true">
            type here
         </body>
      </html>
   </iframe>
</div>

采纳答案by Ali Hamadi

the problem was solved by finding the iFrame by xpath

通过 xpath 查找 iFrame 解决了问题

driver.switchTo().defaultContent();
driver.switchTo().frame( driver.findElement( By.xpath( iframeXpath ) ) );

and then return to the top window:

然后返回顶部窗口:

 driver.switchTo().defaultContent();

回答by mfsi_krushnas

You cant select an iFrame using class.Check the webdriver documentation using : -

您不能使用 class.Check webdriver 文档使用:-

https://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/WebDriver.TargetLocator.html

https://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/WebDriver.TargetLocator.html

  1. You can switch to iFrame by 'Name'/'id' attribute.

    driver.switchTo().frame("frame1");
    
  2. You can switch by frame index.

    driver.switchTo().frame(driver.findElements(By.tagName("iframe").get(0));
    
  1. 您可以通过 'Name'/'id' 属性切换到 iFrame。

    driver.switchTo().frame("frame1");
    
  2. 您可以按帧索引切换。

    driver.switchTo().frame(driver.findElements(By.tagName("iframe").get(0));
    

Now you have to check in the whole page how many iFrames are present ?? If its say : 3, use the 3rd iFrame always.

现在你必须检查整个页面有多少 iFrame ?如果它说:3,请始终使用第 3 个 iFrame。

driver.switchTo().frame(driver.findElements(By.tagName("iframe").get(3));

I hope it helps.

我希望它有帮助。

回答by mfsi_krushnas

Yes, there was a mistake..Ok you can do one thing..Manually count how many iframe are there in the page, if its 3rd one whre you want to switch.

是的,有一个错误..好吧,你可以做一件事..手动计算页面中有多少 iframe,如果它的第 3 个 whre 你想切换。

Directly specify the int value as doc says,

如doc所说,直接指定int值,

driver.switchTo().frame(index)

So your code can become something like this : -

所以你的代码可以变成这样:-

driver.switchTo().frame(3);

And dont forget to get back to default Content.

并且不要忘记回到默认内容。

driver.switchTo().defaultContent();

Please let me know if that works or not.

请让我知道这是否有效。