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

标签 javascript java selenium getelementbyid selenium-firefoxdriver

我遇到这个问题已经有一段时间了。浏览了 SO 和其他论坛上的很多主题,但仍然一无所知。

在 Eclipse Neon v2 IDE 中使用 Selenium 3.4.0、geckodriver v0.16.1 和 Mozilla Firefox 53.0 在 Web 应用程序上自动化简单流程​​时,我在控制台上间歇性地遇到错误,如下所示:

JavaScript error: https://www.url.com/my, line 1715: TypeError: document.getElementById(...) is null

虽然使用 chromedriver v2.29/Google Chrome 58.0 或使用 Python,但我没有遇到任何此类问题。

一旦出现此错误,测试执行将停止并最终显示 TimeoutException,如下所示:

Exception in thread "main" org.openqa.selenium.TimeoutException: Timeout loading page after 300000ms

网站的网址是:https://www.shareinvestor.com/my

HTML DOM 是:

<div id="sic_sitemap">
  <div id="sic_container">
    <div id="sic_header">
      <h1 id="sic_siteTitle">
        <div id="sic_headerMembershipLink">
          <a id="sic_mobileEdition" href="/mobile">
            <div id="sic_loginContainer" class="sic_logIn" style="">
              <div class="sic_login-wrap">
                <div class="sic_logIn-subscribe">
                  <div class="sic_logIn-bg">
                    <a href="/user/login.html">
                  </div>
                </div>
              </div>
            </div>
            <div id="sic_subHeader">
              <div id="sic_mainNav" class="sic_withRightCorner">
                <div id="sic_sideBar" class="sic_expanded { selected_market_suffix: 'MY'}">
                  <div class="sic_superBanner sic_superBannerTop">
                    <div id="sic_content" class="sic_collapsed">
                      <div id="sic_footer" class="si_fixed">
                      </div>

截至目前,我已经尝试了以下选项但无济于事:

  1. Java 点击
  2. JavascriptExecutor 点击
  3. 点击操作

这是我的代码:

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.remote.DesiredCapabilities;

public class 78644072022 {

public static void main(String[] args) {

    System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
    DesiredCapabilities dc = DesiredCapabilities.firefox();
    dc.setCapability("marionette", true);
    WebDriver driver =  new FirefoxDriver(dc);
    driver.manage().window().maximize();
    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();

    //JavascriptExecutor Click
    /*JavascriptExecutor jse = (JavascriptExecutor)driver;
    jse.executeScript("arguments[0].click();", login_button);*/

    //Actions Click
    /*Actions act = new Actions(driver);
    act.moveToElement(login_button).click().build().perform();*/

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

我正在寻找一个带有 geckodriver 的 Java 解决方案来克服错误 JavaScript error:TypeError: document.getElementById(...) is null

SO threads 之一中我看到了这样的解决方案:

You need to do a null check in updateHTML like this:

function updateHTML(elmId, value) {
  var elem = document.getElementById(elmId);
  if(typeof elem !== 'undefined' && elem !== null) {
    document.getElementById(elmId).innerHTML = value;
  }
}

我们可以实现吗? 任何建议和指示都会有所帮助。

最佳答案

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

为了回答您的问题,我认为您无法通过 Java/Selenium 做任何事情来“修复”这个问题。这是一个 JavaScript 错误,源自您尝试访问的网站。您可能需要考虑 contacting他们的支持团队,也许他们的开发人员之一可以看看这个问题。


与其点击登录按钮,不如考虑直接导航到登录页面?

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

关于javascript - Selenium-Java-geckodriver : Test Execution halts with JavaScript error as "document.getElementById(...) is null",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44158290/

相关文章:

用于解决性能问题的 Java 分析器

java - 使用自签名证书将 J2ME 应用程序连接到 https

java - Selenium 代码打开 facebook Messenger 并发送消息

python - Scrapy InIt self.initialized() -- 没有初始化

css - 以下 html 片段需要 xpath 或 css

javascript - 将事件处理程序附加到 JQuery 生成的 DOM 元素

java - 如何使用 Junit 模拟 Builder

javascript - AngularJS 将数据传递到模板 View

javascript - 防止输入字段显示与正则表达式不匹配的字符

javascript - 防止在禁用 js 的情况下加载页面