C# FindElement By.Id or XPath both doesn't find element in simple html page

96 views
Skip to first unread message

FVA

unread,
Oct 12, 2016, 10:42:32 PM10/12/16
to Selenium Users
Hi,

I have been trying different things to get this Selenium to work but no luck.
I created a new WPF project and downloaded the Selenium Nugget packages V2.53.1 Support and WebDriver.
This is the code I use:
            IWebDriver driver = new InternetExplorerDriver("C:\\Program Files\\SeleniumWebPagetester");
            driver.Navigate().GoToUrl("D:\\test.html");
            var src = driver.PageSource; //showes the html page -> works
            var test = driver.FindElement(By.Id("aspnetForm")); //An unhandled exception of type 'OpenQA.Selenium.NoSuchElementException' occurred in WebDriver.dll
            var testy = driver.FindElement(By.Id("aspnetForm"), 30); //'OpenQA.Selenium.NoSuchElementException'
            var tst = driver.FindElement(By.XPath("//*[@id=\"lx-home\"]"), 30); //'OpenQA.Selenium.NoSuchElementException'
            driver.Quit();

The web page:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
    </head>
    <body>
<form action="#" id="aspnetForm" onsubmit="return false;">
<section id="lx-home" style="margin-bottom:50px;">
 <div class="bigbanner">
<div class="splash mc">
 <div class="bighead crb">LEAD DELIVERY MADE EASY</div>
</div>
 </div>
</section>
</form>
    </body>
</html>


Things I tried:
The timer, I waited 30 seconds before it starts looking for the ID to be sure the page is loaded -> No element exception
PageSource, I checked if the html page was loaded correctly. The full page gets correcty loaded, no issue here imo.
Xpath, also doesn't find any id or class.
Xpath gives the correct element when I use the HTMLWeb Class -> Xpath WORKS
            string Url = "D:\\test.html";
            HtmlWeb web = new HtmlWeb();
            HtmlDocument doc = web.Load(Url);
            var element = doc.DocumentNode.SelectNodes("//*[@id=\"lx-home\"]"); //WORKS

What am I doing wrong here?

Capture.PNG
Capture.PNG

Jim Evans

unread,
Oct 13, 2016, 2:03:48 AM10/13/16
to Selenium Users
You're attempting to load the HTML file directly from your hard drive, without using a web server. The IE driver does not support this. Use a web server to host the file.

FVA

unread,
Oct 13, 2016, 3:15:57 AM10/13/16
to Selenium Users
I tried Google.com and I get the NoSuckWindowExeption on driver.PageSource.
driver.Navigate().GoToUrl("http://www.google.com");

FVA

unread,
Oct 13, 2016, 4:49:14 AM10/13/16
to Selenium Users
 Using Chrome, hosted html file and moving the driver out of program files did the trick. 
Reply all
Reply to author
Forward
0 new messages