简体   繁体   English

Selenium IDE-TypeError:document.getElementById(..)为空

[英]Selenium IDE - TypeError: document.getElementById(..) is null

The situation is, that I want to use "runScript" in order to send a xmlhttprequest in the beginning when the website gets opened, find an element in the responseText, see if a specific textcontent is present and if so, I want to click on that element. 情况是,我想使用“ runScript”以便在打开网站时开始发送xmlhttprequest,在responseText中找到一个元素,查看是否存在特定的textcontent,如果存在,我要单击该元素。

I am not allowed to show you the source of the website, but I hope your still able to (maybe) find the mistake. 我不允许向您显示网站的来源,但希望您仍然能够(也许)发现错误。

My script: 我的剧本:

javascript{
var req = new XMLHttpRequest();
req.addEventListener("load", function(event) {
 if(req.status >= 200 && req.status < 300)
 {
   var parser = new DOMParser();
   var xmlDoc = parser.parseFromString(req.responseText, "text/html");
   if(document.getElementById("application_widgets__0_selectedUserLanguage_label").childNodes[0].textContent=="English")
   {
     document.getElementById("application_widgets__0_selectedUserLanguage_label").parentNode.click()
   }
 }
 });
req.open("GET", "..............................");
req.send();
}

I've already tested it in the console of firebug before putting it into the Selenium IDE and it works perfectly. 在将它放入Selenium IDE之前,我已经在firebug控制台中对其进行了测试,并且效果很好。 But I always get the same exception when running it in Selenium IDE: 但是在Selenium IDE中运行它时,总是会遇到相同的异常:

在此处输入图片说明

My question is: why does it work in firebug but not in selenium IDE and what did I do wrong? 我的问题是:为什么它只能在Firebug中工作,而不能在Selenium IDE中工作,我怎么做错了?

  • no there's no iframe 不,没有iframe

Thanks for the answers in advance! 预先感谢您的答复! :) :)

This indicates that the page is not fully loaded yet when you call document.getElementById() . 这表示当您调用document.getElementById()时,页面尚未完全加载。 You need to wrap your code in a window.addEventListener("load", function() { ... }); 您需要将代码包装在window.addEventListener("load", function() { ... }); to get it to work. 使它工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM