c# - 无法使用 PhantomJS、Selenium 通过 XPath 将文本发送到 iFrame 元素

标签 c# selenium iframe xpath phantomjs

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

但是,当我切换到 PhantomJS 时,测试运行但 key 从未输入到 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 .

有问题的特定文本字段(快速回复字段): enter image description 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。

最佳答案

制作 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!");

关于c# - 无法使用 PhantomJS、Selenium 通过 XPath 将文本发送到 iFrame 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38821386/

相关文章:

php - 使用 PHP 文件加载外部网站

c# - 程序集无法在受限制的 AppDomain 中加载

c# - Ninject 2.0 构造函数参数 - 当默认构造函数也存在时如何设置?

Python、Selenium Webdriver : How can I find the value of this attribute?

selenium - 使用 Selenium 修改innerHTML

javascript - 访问iframe的顶部html和body标签

c# - Convert.FromBase64String c# 上的 FormatException

c# - 如何在 Windows Phone 8.1 上打开 iBoxDb 服务器

javascript - Codeception 中的 jQuery 未定义

javascript - 在父级中使用 javascript 编辑 iframe 的 DOM