[英]How to find web element by xpath using javascript code in python selenium webdriver?
I would like to find web element by xpath executing javascript code in python and then click on it.我想通过在 Z23EEEB4347BDD26BFC6B7EE9A3B755DD 中执行 javascript 代码的 xpath 找到 web 元素,然后单击它。 Is this possible and how to do this?这可能吗?如何做到这一点?
To find a WebElement by xpath using javascript in you have to use the evaluate()
method which evaluates an xpath expression and returns a result.要在xpath中使用javascript查找WebElement ,您必须使用评估xpath表达式并返回结果的evaluate()
方法。
document.evaluate() returns an XPathResult based on an XPath expression and other given parameters. document.evaluate()基于XPath表达式和其他给定参数返回XPathResult 。
The syntax is:语法是:
var xpathResult = document.evaluate(
xpathExpression,
contextNode,
namespaceResolver,
resultType,
result
);
As an example the Search Box within the Google Home Page which can be identified uniquely using the xpath as //*[@name='q']
can also be identified from the google-chrome-devtools Console using document.evaluate()
and the xpath expression as follows:例如,谷歌主页中的搜索框可以使用xpath作为//*[@name='q']
进行唯一识别,也可以使用document.evaluate()
从google-chrome-devtools控制台识别xpath表达式如下:
document.evaluate("//*[@name='q']", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
Snapshot:快照:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.