我希望使用 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 .
如有任何帮助,我们将不胜感激。
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/