【问题标题】:Unable to send text through XPath to an iFrame element using PhantomJS, Selenium无法通过 XPath 将文本发送到使用 PhantomJS、Selenium 的 iFrame 元素
【发布时间】:2016-08-08 04:38:32
【问题描述】:

我希望使用 Selenium 将文本字符串发送到 iFrame 元素。 以前,我可以通过使用 Firefox 驱动程序来实现。

但是,当我切换到 PhantomJS 时,测试运行但从未将密钥输入到 iFrame 文本框中。

代码如下:

driverJS.SwitchTo().Frame(driverJS.FindElement(By.XPath("/html/body/div[1]/div[2]/div[9]/form/div[3]/div[1]/div/div/div/div/div/span/span[2]/span/table/tbody/tr[2]/td/iframe")));
//Switch to iFrame and locate element.

driverJS.FindElement(By.XPath("/html/body")).SendKeys("bump this up!");
//Send keys to /html/body xpath of iFrame

driverJS.SwitchTo().DefaultContent();
//Switch out of iFrame

网址:here.

有问题的特定文本字段(快速回复字段):

任何帮助将不胜感激。

iframe 正文的原始 HTML,它没有名称,因此我使用 xpath:

<iframe frameborder="0" allowtransparency="true" tabindex="1" src="" title="Rich text editor, vB_Editor_QR_editor, press ALT 0 for help." style="width:100%;height:100%">

我尝试使用以下代码搜索 iframe 的帧索引:

    System.Console.WriteLine("The total number of iframes are " + iFramList.Count());

    foreach (IWebElement i in iFramList)
    {
        if (driverJS.FindElement(By.XPath("/html/body/div/div[2]/div[9]/form/div[3]/div[1]/div/div/div/div/div/span/span[2]/span/table/tbody/tr[2]/td/iframe")).Displayed)
        {
            System.Console.WriteLine(i);
        }
    }

我得到的输出是:

The total number of iframes are 12
OpenQA.Selenium.Remote.RemoteWebElement
OpenQA.Selenium.Remote.RemoteWebElement
OpenQA.Selenium.Remote.RemoteWebElement
OpenQA.Selenium.Remote.RemoteWebElement
OpenQA.Selenium.Remote.RemoteWebElement
OpenQA.Selenium.Remote.RemoteWebElement
OpenQA.Selenium.Remote.RemoteWebElement
OpenQA.Selenium.Remote.RemoteWebElement
OpenQA.Selenium.Remote.RemoteWebElement
OpenQA.Selenium.Remote.RemoteWebElement
OpenQA.Selenium.Remote.RemoteWebElement
OpenQA.Selenium.Remote.RemoteWebElement

考虑到这很奇怪,没有 foreach 循环的运行会产生 13 个 iframe。

【问题讨论】:

  • 你想在哪里设置值in this link..??
  • 嗨,对不起,在“快速回复”iframe 文本框中。
  • 这个“快速回复”链接在哪里。我看不到..你能分享截图吗??
  • 我已编辑问题以包含文本字段的图像。谢谢

标签: c# selenium iframe xpath phantomjs


【解决方案1】:

制作 iframe 列表并尝试从那里调用开关:

//look at the list in debug mode and find the iframe index
IList<IWebElement> iFramList = driverJS.FindElement(By.TagName("iframe"));

driverJS.SwitchTo().Frame(index);

//after that you should send the text to textBox not the body, inspect the element and defind it by id or name like in that example

driverJS.FindElement(By.XPath("/html/body")).SendKeys("bump this up!");
//Send keys to /html/body xpath of iFrame

driverJS.FindElement(By.Id("<your textBoxId>")).SendKeys("bump this up!");

或按名称

driverJS.FindElement(By.Name("<your textBoxName>")).SendKeys("bump this up!");

【讨论】:

  • 嗨,我遇到了错误“'OpenQA.Selenium.IWebElement' to 'System.Collections.Generic.IList'。存在显式转换(你是缺少演员表?)”当我进入您建议的第一行时。
  • @user4985,将第一行的FindElement替换为FindElements
  • 谢谢,它有效。我写了一个控制台调试输出,它显示页面上有 13 个 iframe。我会一一尝试找出我的特定 iframe 吗?
  • 你可以通过innerHtml查看对象,或者让它动态尝试通过执行foreach循环来找到你知道存在于框架中的对象,当它找到它时会打破循环并返回索引
  • 感谢您的回复,我已尝试按照建议使用 foreach 循环,并使用结果输出更新了问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-01-20
  • 1970-01-01
  • 2022-08-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-05
相关资源
最近更新 更多