【问题标题】:How to identify and switch to the frame in selenium webdriver when frame does not have id and title当框架没有id和标题时如何识别并切换到selenium webdriver中的框架
【发布时间】:2015-02-08 12:25:03
【问题描述】:

谁能告诉我如何识别并切换到没有标题和 ID 的 iframe。 我需要进入这个框架,以便在这个框架中找到另一个元素来发送一些文本。

<div class="sceditor-container" style="width: 1359px; height: 300px;">
  <div class="sceditor-toolbar">
  <iframe frameborder="0" src="javascript:false" style="width: 1349px; height: 261px;">
     <!DOCTYPE html>
     <html style="height:100%">
       <head>
             <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
             <link href="" type="text/css" rel="stylesheet">
       </head>
      <body contenteditable="true" style="height:88%">
        <div> </div>
      </body>
     </html>
 </iframe>

为此我写了代码:

WebDriver.SwitchTo().Frame(WebDriver.FindElement(By.TagName("iframe")));
WebDriver.FindElement(By.XPath("html/body")).SendKeys("text");
WebDriver.SwitchTo().DefaultContent();

但 WebDriver 找不到 iframe 元素,因此无法将文本值发送到 iframe 元素。

【问题讨论】:

  • 是否抛出了与“No Frame found”相关的异常?

标签: c# selenium


【解决方案1】:

点击和发送键使用下面的代码

WebElement loc=driver.findElement(By.XPath("html/body"));
 Actions a= new Actions(driver);
 a.click(loc).sendKeys("emailBody").perform();

要在 iframe 元素中设置文本,您必须首先从 Body 标签中复制文本

WebElement emailBody = driver.findElement(by.xpath("/html/body"));
emailBody.getText();

将文本复制到 emailBody 后,您可以将其与框架内的元素一起使用,如下所示

WebDriver.FindElement(By.XPath("html/body")).SendKeys(emailBody);

尝试下面的代码切换到框架,看看会发生什么

driver.switchTo().frame(driver.findElement(By.xpath("//iframe[contains(@src,'javascript:false')]")));

你也可以尝试使用它的索引来访问框架,试试

driver.switchTo().frame(0);

【讨论】:

  • 等待一段时间后,我收到:OpenQA.Selenium.WebDriverTimeoutException:60 秒后超时 OpenQA.Selenium.NoSuchElementException:无法找到元素:{"method":"xpath","selector": "//div/div/span[contains(text(), 'Preview')]/../.."} 所以这个元素的代码是:WebDriver.SwitchTo().Frame(WebDriver.FindElement(By. XPath("//iframe[contains(@src,'javascript:false')]"))); WebDriver.FindElement(By.XPath("html/body")).SendKeys(emailBody); WebDriver.SwitchTo().DefaultContent();
  • 我在上面发布了 HTML,在我的问题中
  • 啊是的 Prview 是我在 iframe 中填充此元素后应该单击的按钮。但它收到错误,因为验证说:“需要电子邮件正文”。所以我需要填充这个 iframe 元素,但正如我提到的,我找不到它
  • Webdriver 似乎可以找到这个 iframe 元素,但 webdriver 无法在这个元素中设置文本...
  • 目前不可用。回答您的前任问题:电子邮件正文是用户可以在他应该填写的表格上方看到的文本。所以 iframe 元素是页面上用户应该设置一些文本内容的地方。所以代码 WebDriver.SwitchTo().Frame(0) 或 WebDriver.SwitchTo().Frame(WebDriver.FindElement(By.XPath("//iframe[contains(@src,'javascript:false')]")) ) 在职的。但问题是Web驱动不能在iframe中设置文本:WebDriver.FindElement(By.XPath("html/body")).SendKeys(emailBody);
【解决方案2】:

我使用了一段时间的 ChromeDriver,但最近切换到了 phantomjs,因为我遇到了 ChromeDriver 莫名其妙地挂起的问题。 phantomjs(如果我的研究是正确的)几乎完全通过 Javascript 通过(这更像是猜测而不是事实)注入来工作。那么,这有什么关系呢?好吧,我发现您可以针对已加载页面的 DOM 运行自己的 JS。所以我把你的源代码扔进了https://www.freeformatter.com/xpath-tester.html#ad-output,并试图对它运行一些xpath,结果它抛出了。在使用正斜杠关闭元和链接标签后,它然后为开始对添加关闭 div 标签,然后按预期评估我的 xpath。
你能纠正这些还是这个来源对你来说是外部的?如果可以,我会这样做。 然后尝试使用

运行 Jscript
driver.ExecuteScript("Your javascript here..");  

将代码设置为脚本中的 div 对象的值。我手边没有示例,但使用适当的选择器和

driver.querySelector("tag.className..").innerHTML = "Your Keys";

在使用 Chrome 中的控制台来解决这个问题时,我仍然遇到问题。但我强烈怀疑格式错误的 HTML 是你的罪魁祸首。如果你已经解决了这个问题,我很乐意看到答案。干杯!

【讨论】:

    猜你喜欢
    • 2013-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-03
    相关资源
    最近更新 更多