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

时间:2018-07-30 13:24:39

标签: 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;");

注意:如果我使用刷新操作,则应用程序将导航回到上一个门户(这是应用程序的行为)。

有没有办法获取元素?

1 个答案:

答案 0 :(得分:0)

将变异观察者用于上述任务。

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

当添加了某些元素或更改了任何属性或更改了文本时,观察者将通知您。如果您只想观察孩子,只需删除其他两个孩子即可。