如何使用Javascript获得刷新和新的DOM元素



我正在使用Web应用程序自动使用Javascript和selenium。有一个下拉列表,它有不同的门户,如果我更改下拉列表中的值,整个页面将根据我们选择的门户而更改。比如,开发人员门户、测试人员门户等,。。但URL是相同的。

我可以很容易地更改门户,但如果我在更改门户后尝试获取元素,我将获得以前的门户元素。

我正在使用JS和硒,

String id = (String)jse.executeScript("var x = document.getElementsByTagName(""+ tag +"");var e=''; for(i=0; i<x.length; i++) { e+=x[i].innerText + ",";if(x[i].innerText.trim()===""+searchObject+""){x[i].click();}} return e;");

注意:如果我使用刷新操作,应用程序将导航回以前的门户(这是一种应用程序行为(。

有没有办法获得元素?

对上述任务使用突变观测器。

// select the target node
var target = document.querySelector('#some-id');
// In your case this will body or the parent where you are adding elements
// create an observer instance
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
console.log(mutation.type);
});
});
// configuration of the observer:
var config = { attributes: true, childList: true, characterData: true }
// pass in the target node, as well as the observer options
observer.observe(target, config);

当添加了一些元素、更改了任何属性或更改了文本时,观察者会通知您。如果你只想观察孩子,就去掉另外两个。

最新更新