这里简单笔记一下如何使用xpath定位,并使用javascript执行功能。
function xpath(path){
return document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
}
# 定义一下函数xpath, 后续调用函数就简洁很多。
input_text = xpath("//input[@id='kw']")
1、用js去滑到指定文本那里去:
document.evaluate('xpath定位表达式', document).iterateNext().scrollIntoView();
document.querySelector("JS path定位表达式").scrollIntoView();
2、js点击
document.querySelector("JS path定位表达式").click();
document.evaluate('xpath定位表达式', document).iterateNext().click();
3、js获取value值
return document.querySelector("JS path定位表达式").value();
return document.evaluate('xpath定位表达式', document).iterateNext().value();
4、将xpath定位方式转化为JS path定位方式
document.evaluate('xpath定位表达式', document).iterateNext()
5、 页面输入框改为敏感类型
copyjs path.setAttribute("type","password")
6、 页面输入框改为明文
copyjs path..setAttribute("type","text")
7、 js移除<input>readOnly属性
document.querySelector("JS path定位表达式") removeAttribute("readOnly");
document.evaluate('xpath定位表达式', document).iterateNext().removeAttribute("readOnly");
document.evaluate("//*[@id='RequisitionDate']/div/input",document).iterateNext().removeAttribute('readonly')
# 移除输入框的只读状态。例如日期需要点击日历才能选定的,这样就可以直接填写日期了。