无法在 selenium webdriver 中选择 Iframe [英] Can't select an Iframe in selenium webdriver

查看:27
本文介绍了无法在 selenium webdriver 中选择 Iframe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过类名选择 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" );

这是我网页中的 HTML:

<div id="cke_534_contents" class="cke_contents cke_reset" role="presentation" style="height: 75px;"><span id="cke_586" class="cke_voice_label">按ALT 0寻求帮助</span><iframe class="cke_wysiwyg_frame cke_reset" frameborder="0" src="" style="width: 100%; height: 100%;"aria-scribedby="cke_586" tabindex="0" allowtransparency="true"><!DOCTYPE html><html lang="en-gb" dir="ltr"><头><body class="cke_editable cke_editable_themed cke_contents_ltr cke_show_borders" contenteditable="true" spellcheck="true">在此输入</iframe>

解决方案

通过xpath找到iFrame解决了问题

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

然后返回顶部窗口:

 driver.switchTo().defaultContent();

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

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:

<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>

解决方案

the problem was solved by finding the iFrame by xpath

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

and then return to the top window:

 driver.switchTo().defaultContent();

这篇关于无法在 selenium webdriver 中选择 Iframe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆