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

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


I am looking for a Java Solution with geckodriver to overcome the error JavaScript error:TypeError: document.getElementById(...) is null

To answer your question, I don't believe there's anything you can do to "fix" this via Java/Selenium. This is a JavaScript error, which originates from the website that you are trying to access. You might want to consider contacting their support team, maybe one of their developers can look at this issue.


Instead of clicking on the login button, maybe consider navigating directly to the login page?

driver.get("https://www.shareinvestor.com/user/login.html");


Firstly, Those javascript errors are not triggered due to any of the selenium code. Ofcourse, the timeout has been triggered by the selenium(will discuss on this on a later point).

You will get that javascript error irrespective of any kind of browser you launch the URL with. But in case of the gecko, you are notified in the eclipse console with but not in case of Chrome. If you need to see the java script error in chrome, just launch the url in chrome and go to devtools/console(F12). You can also see the same in firefox console too.

  • Chrome Img:

Error shown in Chrome console

Secondly, We are getting timeout exception because the site is really taking too much time to load. I have been waited for 7 minutes and the page is still loading even now. Selenium won't executes its script unless the page has been completely launched. As a result we are getting timeout exception(not sure about the default page launch time permitted). I thought of bypassing directly to the login page ("https://www.shareinvestor.com/user/login.html") and that's also not taking any finite time to load completely.

Intepolation: Those java script errors are not an issue for automation But those page loads are really. This site doesn't seems like a good candidate for automation with this issue.

Update1: otherwise we can also stop the page loading via another thread like sending "esc" key sequence using Action class.

Update2: I tried the same code today and it works fine. Below is the code snippet that i have tried(There is no change at all)

 public static void main(String[] args) throws InterruptedException {    System.setProperty("webdriver.gecko.driver", "F:\\Softwares\\Selenium\\Webdriver\\geckodriver.exe");    WebDriver driver = new FirefoxDriver();    driver.manage().window().maximize();    driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);    driver.get("https://www.shareinvestor.com/my");    WebElement login_button = driver.findElement(By.xpath("//div[@id='sic_loginContainer']/div/div[@class='sic_logIn-bg']/a"));    //Java Click    login_button.click();    System.out.println("In Login PAge");    driver.findElement(By.xpath("//input[@id='sic_login_header_username']")).sendKeys("debanjan");    driver.findElement(By.xpath("//input[@id='sic_login_header_password']")).sendKeys("5786");    System.out.println("Entered password");    driver.findElement(By.xpath("//input[@id='sic_login_submit']")).click();}

Selenium version - 3.4.0

Gecko driver - v0.16.1(mine is 32 bit)

Mozilla - 51.0.1 (Update=>It's working on 53.02 also)

Hope this helps you. Thanks.


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();