user1701153 user1701153 - 1 month ago 15
C# Question

NoSuchElementException using PhantomJS 1.9.2 in Visual Studio/C#

I've read all prior posts and tried all suggestions. I've verified the page is loading via taking a screenshot. It's there. I have a 30 second implicit wait in place, which PhantomJS waits for. I have a fully qualified URL. And i've tested the same simple code snippet below with ChromeDriver and it works by flipping to that driver. Any ideas?

[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
var d = new PhantomJSDriver();
//var d = new ChromeDriver();

try
{
var s = "abc";
d.Manage().Window.Maximize();

d.Manage().Timeouts().ImplicitlyWait(System.TimeSpan.FromSeconds(10));
d.Navigate().GoToUrl("http://www.google.com");

var e = d.FindElement(By.Name("q"));
e.SendKeys(s);
e.Submit();

Assert.AreEqual<String>(s + " - Google Search", d.Title);

}
catch(Exception e)
{
((ITakesScreenshot)d).GetScreenshot().SaveAsFile("c:\\exception.png", System.Drawing.Imaging.ImageFormat.Png);
Console.WriteLine(d.PageSource);
throw e;
}
finally
{
d.Quit();
}
}


PhantomJS Output Window

Answer

Google may response with different HTML code depending on user agent and/or viewport size. Set a viewport of a desktop browser (like 1280x1024) to get a desktop version, whereas default PhantomJS viewport of 400x300 gets a lighter mobile version.

On how to change viewport size, see http://stackoverflow.com/a/23840494/2715393