2016-08-08 23 views
2

我正在寻找使用Selenium将文本字符串发送到iFrame元素。以前,我可以通过使用Firefox驱动程序来实现这一点。无法使用PhantomJS通过XPath发送文本到iFrame元素,Selenium

但是,当我切换到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。

+0

要设置值在此链接]在哪里(HTTP://论坛.vr-zone.com /其他硬件组件/ 3382325-海盗-scimitar.html)..? –

+0

嗨,对不起,在“快速回复”iframe文本框中。 – user4985

+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!"); 
+0

嗨,我遇到了一个错误“'OpenQA.Selenium.IWebElement'到'System.Collections.Generic.IList '。显式转换存在(你是否缺少一个转换?)”当我发明你的建议的第一行。 – user4985

+0

@ user4985,在第一行用'FindElements'替换FindElement。 –

+0

谢谢,它的工作原理。我编写了一个控制台调试输出,它显示页面上有13个iframe。我是否一个接一个地尝试弄清我的特定iframe? – user4985

相关问题