How to Ignore JavaScript Exceptions When Working with Webdriver (Htmlunit, Ruby Bindings)

Is it possible to ignore JavaScript exceptions when working with WebDriver (HtmlUnit, Ruby bindings)

After looking at the source of the HtmlUnitDriver, it seems like there is no possibility to customize the behaviour you want to change. The easiest thing you could do to solve this is to patch and recompile the Selenium server (which might or might not be an option). You'd need to add this line:

--- HtmlUnitDriver.java 2012-01-05 17:45:22.779579136 +0100
+++ HtmlUnitDriver.java 2012-01-05 18:14:51.415106195 +0100
@@ -255,6 +255,7 @@
WebClient client = newWebClient(version);
client.setHomePage(WebClient.URL_ABOUT_BLANK.toString());
client.setThrowExceptionOnFailingStatusCode(false);
+ client.setThrowExceptionOnScriptError(false);
client.setPrintContentOnFailingStatusCode(false);
client.setJavaScriptEnabled(enableJavascript);
client.setRedirectEnabled(true);

Build selenium-server-standalone executable jar

The best way I found is to do:

go //java/server/src/org/openqa/selenium/remote/server:server:uber //java/client/src/org/openqa/selenium:client-combined:uber

This will create a single jar for the selenium server

Then run go release.

The resulting .jar should be found in build\dist folder of the selenium source folder. (ex. ./build/java/server/src/org/openqa/grid/selenium/selenium-standalone.jar)

See also https://github.com/SeleniumHQ/selenium/wiki/Building-WebDriver#tips.

How to detect when Selenium loads a browser's error page

WebDriver API doesnot expose HTTP status codes , so if you want to detect/manage HTTP errors, you should use a debugging proxy.

See Jim's excellent post Implementing WebDriver HTTP Status on how to do exactly that.

Selenium-Java-geckodriver : Test Execution halts with JavaScript error as document.getElementById(...) is null

I think I've managed to find what is causing this uproar in your script.
I inspected your HTML and it seems javascript method function showRemaining() is causing this problem; because showRemaining() contains this statement

    document.getElementById('countdown').innerHTML = '';

where it tries to set innerHTML attribute for element having id as countdown to ''.

But countdown doesn't exist anywhere on your web page hence the error.

TypeError: document.getElementById(...) is null

and somehow selenium isn't able to look past this error. So I think getting it fixed from developers should help you.

UPDATE :

Basically you need to wait all elements to load using implicit wait, once all elements are loaded, then your Javascript error gets resolved and ultimately interaction with the webpage is not hindered by that javascript error:

        driver.get("https://www.shareinvestor.com/my");

driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.manage().window().maximize();

/*String script = "function updateHTML(countdown, value) {" +
"var elem = document.getElementById('countdown');"+
"if(typeof elem !== 'undefined' && elem !== null) {"+
" document.getElementById('countdown').innerHTML = value;"+
"}"+
"}";

((JavascriptExecutor) driver).executeScript(script);*/

WebElement login_button = driver.findElement(By.xpath("//div[@id='sic_loginContainer']/div/div[@class='sic_logIn-bg']/a"));

login_button.click();

driver.findElement(By.xpath("//input[@id='sic_login_header_username']")).sendKeys("debanjan");
driver.findElement(By.xpath("//input[@id='sic_login_header_password']")).sendKeys("5786");
driver.findElement(By.xpath("//input[@id='sic_login_submit']")).click();


Related Topics



Leave a reply



Submit