为什么 Selenium 网络驱动程序无法从 aspx 网页获取 'tags'?

Why Selenium web-driver is not able to get 'tags' from aspx web page?

Selenium 无法获得任何 标签,例如“table”或“a" 来自 asp 页。我还尝试获取“body”标签,在这种情况下它可以识别但抛出异常如下:

> 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"));
        }

frameset count it gives zero and rows length 1.

显然,当我单击查看页面源并将该页面另存为 html 时。我可以获得标签。

任何帮助,我在这里缺少什么?

EDIT :: 我尝试获取网页 selenium 获取的网页。它 returns :`

> <!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>

`

但是当我手动检查页面源代码时,它看起来很正常,应该是这样。

您必须使用 switchTo 切换到 iframe。 参见 http://docs.seleniumhq.org/docs/03_webdriver.jsp

driver.switchTo().frame("frameName");

已解决: 这是因为在 Firefox 中使用 IE 选项卡打开页面。 Firefox Selenium 附加组件在那里不起作用。需要打开 IE 驱动程序并将配置文件设置为 dontNet Client.