Search code examples
c#jqueryselenium-webdriverhtmlunit

Selenium Error when using JavaScript or getting elements


Using Seleneium 2.25, I've had a lot of issues arise.

I'm trying to use Selenium Remote Driver on a remote machine (Server) from my computer (local / client). However, when I try to use DesiresCapabilities.Htmlunit() It will locate the elements, but it says they are not visible. I'm completely stumped by this. I'm not sure why it can be found but then not visible.

So then I tried to use some JavaScript in order force it. It comes back and throws an error saying that the webpage can not execute javascript before the page is loaded. How is this possible when I did an implicate wait, and it found the element it was waiting for?

DesiredCapabilities iecapa = DesiredCapabilities.HtmlUnit();
iecapa.IsJavaScriptEnabled = true;
driver = new RemoteWebDriver(new Uri("http://<IP of server>:4444/wd/hub"), iecapa);

IJavaScriptExecutor jQuery = ((IJavaScriptExecutor)(driver));
addressElement = (IWebElement)jQuery.ExecuteScript("return document.GetElementByName('searchAddress')");

So if anyone would like to help me, it would be greatly appreciated! Thank you!

http://imageshack.us/photo/my-images/163/seleniumhtmluniterror.jpg/

that is the error. StackOverflow wont let me post it here. =(


Solution

  • Try this:

    WebDriverWait wait = new WebDriverWait(driver, new TimeSpan(0, 0, 0, 30));
    wait.Until(p => driver.FindElement(By.Name("searchAddress")));
    
    IJavaScriptExecutor jQuery = ((IJavaScriptExecutor)(driver));
    addressElement = (IWebElement)jQuery.ExecuteScript("return document.GetElementByName('searchAddress')");
    

    Also you can add check in JavaScript:

    if (document.readyState.toLowerCase()=="complete") 
    return document.GetElementByName('searchAddress');
    return 'Error';