2016-08-08 25 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

相關問題