【发布时间】:2017-10-24 19:01:27
【问题描述】:
我遇到这个问题已经有一段时间了。在 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>
到目前为止,我已经尝试了以下选项,但无济于事:
- Java 点击
- JavascriptExecutor 点击
- 点击操作
这是我的代码:
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 之一中,我看到了类似的解决方案:
您需要像这样在 updateHTML 中进行空检查:
function updateHTML(elmId, value) {
var elem = document.getElementById(elmId);
if(typeof elem !== 'undefined' && elem !== null) {
document.getElementById(elmId).innerHTML = value;
}
}
我们可以实现吗? 任何建议和指示都会有所帮助。
【问题讨论】:
-
你完成了 console.log(document) 并检查了它不为空吗?也许您必须通过您使用的 webdriver 通过 window.document.getElementById() 访问(这只是一个想法)
-
@dev 你在哪里使用 document.getElementById() 在你的代码中?
-
@mtizziani 感谢您的快速浏览。你能指导我设置
console.log(document)来检查它不为空吗?谢谢 -
@kushal。我没有使用那个。那是控制台上的错误导致了麻烦。谢谢
-
@dev 你的意思是浏览器控制台?
标签: javascript java selenium getelementbyid selenium-firefoxdriver