无法使用PhantomJS通过XPath将文本发送到iFrame元素,Selenium

时间:2016-08-08 04:38:32

标签: c# selenium iframe xpath phantomjs

我希望使用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

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

1 个答案:

答案 0 :(得分: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!");