2015-02-11 87 views
0

Selenium無法從asp頁面獲得任何標籤,如「」或「a」。我也嘗試過讓「」標籤,在這種情況下,它承認,但如下拋出異常:爲什麼Selenium網絡驅動程序無法從aspx網頁獲取'標籤'?

> Unhandled Exception: OpenQA.Selenium.StaleElementReferenceException: 
> Element bel ongs to a different frame than the current one - switch to 
> its containing frame to use it at 
> OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response 
> erro rResponse) at 
> OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String 
> driverCommandToExecu te, Dictionary`2 parameters) at 
> OpenQA.Selenium.Remote.RemoteWebElement.GetAttribute(String 
> attributeName) 

我用C#代碼:

FirefoxProfile firefoxProfile = new FirefoxProfile(@"C:\Users\username\AppData\Roaming\Mozilla\Firefox\Profiles\rwyq8vbx.default"); 
     IWebDriver driver = new FirefoxDriver(new FirefoxBinary(@"C:\Program Files (x86)\Mozilla Firefox\firefox.exe"), firefoxProfile); 
     driver.Manage().Window.Maximize(); 
     driver.Navigate().GoToUrl(@"http://epmweb.apac.bbc.com/pwa/_layouts/pwa/mytssummary.aspx"); 
     System.Threading.Thread.Sleep(10000); 
     driver.SwitchTo().DefaultContent(); 
     List<IWebElement> frameset = driver.FindElements(By.TagName("iframe")).ToList(); 
     Console.WriteLine(frameset.Count); 
     foreach (var v in frameset) 
     { 
      Console.WriteLine("fName : " + v.GetAttribute("name")); 
     } 
     IWebElement[] rows = driver.FindElements(By.TagName("body")).ToArray(); 
     Console.WriteLine(rows.Length); 
     foreach (var v in rows) 
     { 
      Console.WriteLine("Name : "+v.GetAttribute("id")); 
     } 

框架指望它給零和行的長度1.

顯然,當我點擊查看頁面源&保存該頁面爲html。我能夠獲得標籤。

任何幫助,我在這裏錯過了什麼?

編輯::我試着得到網頁什麼硒得到作爲網頁。它返回:`

> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html 
> xmlns="http://www.w3.org/1999/xhtml"><head><title>My Timesheets - 
> Project Web Access</title><link 
> href="chrome://ietab2/skin/ietab-favicon-iedoc.png" rel="icon" /> 
> </head> <body onload="window.setTimeout(function() { init(); },0);" 
> style="margin:0; padding:0;overflow:hidden"> 
> 
> <script type="text/javascript"> var gIETab = null; 
> 
> function init(){ 
>  gIETab = document.getElementById("IETab2"); 
>  try { 
>   // This is needed to avoid an "Activate Plugin" prompt in Fx 27+. 
>   // It's not documented, thus the try/catch for back-compat 
>   gIETab.QueryInterface(Components.interfaces.nsIObjectLoadingContent).playPlugin(); 
>  } catch(ex) { 
>  } 
> 
>  if(!gIETab || !gIETab.navigate) 
>  {   // Wait for it to show up 
>  window.setTimeout(function() { init(); }, 200);   return;  } 
> 
>  var m=/(\?url=)(\S+)$/.exec(document.location.href); 
>  if(m) 
>  { 
>   var url = decodeURI(m[2]); 
>   if ((url.indexOf("about:") == 0) || (url.indexOf("chrome://") == 0)) 
>    url = 'http://www.ietab.net/ie-tab-v2-documentation?from=' + url; 
>   gIETab.navigate(url); 
>  } } 
> 
> var IETabCalls = { goBack: function() {  gIETab.goBack(); }, 
> 
> goForward: function() {   gIETab.goForward();  }, 
> 
> navigate: function(url) {  gIETab.navigate(url); }, 
> 
> refresh: function() {  gIETab.refresh(); }, 
> 
> stop: function() {  gIETab.stop(); }, 
> 
> saveAs: function() {  gIETab.saveAs(); }, 
> 
> print: function() {   gIETab.print();  }, 
> 
> printSetup: function() {  gIETab.printSetup(); }, 
> 
> printPreview: function() {  gIETab.printPreview(); }, 
> 
> viewSource: function() {  gIETab.viewSource(); }, 
> 
> find: function() {  gIETab.find(); }, 
> 
> cut: function() {  gIETab.cut(); }, 
> 
> copy: function() {  gIETab.copy(); }, 
> 
> paste: function() {   gIETab.paste();  }, 
> 
> selectAll: function() {   gIETab.selectAll();  }, 
> 
> focus: function() {   if (!gIETab) {   // It might not have loaded 
> yet, wait for the control to fully initialize 
>   window.setTimeout(function() { IETabCalls.focus(); }, 100);   } 
>  else {   gIETab.focus();   } } } 
> 
> window.onpageshow = function(e) { if (e.persisted) 
> window.setTimeout(function() { init(); }, 0); } 
> 
> function createIETab() { 
>  var obj = document.createElement("object"); 
>  obj.id = "IETab2"; 
>  obj.style.width = "100%"; 
>  obj.style.height = "100%"; 
>  // Waterfox/x64 example: 
>  // Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:16.0) Gecko/20121026 Firefox/16.0 
>  // Firefox/x32 on Win64: 
>  // Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/17.0 Firefox/17.0 
>  // Firefox/x32 on Win32 
>  // Mozilla/5.0 (Windows NT 6.1; rv:16.0) Gecko/20100101 Firefox/16.0 
>  // 
>  if (window.navigator.userAgent.indexOf("Win64; x64;") != -1) 
>   obj.setAttribute("type", "application/ietab2_x64"); 
>  else 
>   obj.setAttribute("type", "application/ietab2"); 
>  document.body.appendChild(obj); } // Navigation happens later, but create the IE Tab object immediately createIETab(); </script><object 
> id="IETab2" style="width: 100%; height: 100%;" 
> type="application/ietab2"></object> 
> 
> 
> </body></html> 

`

但是,當我手動檢查網頁的源文件,它看起來正常,因爲它應該是。

+0

它不一定是**「iframe」標籤**。它也可以是一個**框架**標籤。您是否注意到了這種可能性? – Subh 2015-02-11 17:47:34

+0

也沒有「框架」標籤。只有在網站上運行時才能獲取標籤。如果我檢查頁面源並將其保存在本地HTML頁面中,我可以獲取標籤。此外,它可以在網站上運行時獲取身份標記,但不能識別其身份或文本。它實際上是一個內部網站。這沒關係,對吧?我猜想啓用了一些安全性當我使用chrome打開相同的鏈接時,會出現錯誤,「至少使用Internet Explorer版本6才能使用Project Web Access」。 – mkkhedawat 2015-02-12 05:01:22

回答

0

解決:它發生,因爲頁面,因此在Firefox中使用IE選項卡中打開。 Firefox Selenium插件在那裏不起作用。一個需要打開IE驅動程序和配置文件設置爲dontNet 客戶端

相關問題