简体   繁体   English

How to find web element by xpath using javascript code in python selenium webdriver?

[英]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?这可能吗?如何做到这一点?

You can use the following method (check the docs ):您可以使用以下方法(查看 文档):

find_element_by_xpath

Then, to click on the element you found, simply use:然后,单击您找到的元素,只需使用:

element.click()

To find a WebElement by using in you have to use the evaluate() method which evaluates an xpath expression and returns a result.要在中使用查找WebElement ,您必须使用评估xpath表达式并返回结果的evaluate()方法。


document.evaluate() document.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
);

Example例子

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 Console using document.evaluate() and the xpath expression as follows:例如,谷歌主页中的搜索框可以使用xpath作为//*[@name='q']进行唯一识别,也可以使用document.evaluate()控制台识别xpath表达式如下:

document.evaluate("//*[@name='q']", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;

Snapshot:快照:

document_evalute_xpath

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

相关问题 Selenium Webdriver xpath找到元素 - Selenium Webdriver xpath to find element 通过 Python ZC49DFB55F06BB406E2C5CA786F5Z3WebDriver 使用 javascript 语句查找 web 元素 - Finding web element using javascript statments through Python Selenium WebDriver 使用Selenium WebDriver和JavaScript从XPath找到Element的文本 - Get text from XPath located Element using Selenium WebDriver with JavaScript 使用 Selenium WebDriver 和 JavaScript 从 XPath 元素获取文本 - Get text from XPath Element using Selenium WebDriver with JavaScript 有没有办法在 Selenium WebDriver 中使用 JavaScript 通过 XPath 获取元素? - Is there a way to get element by XPath using JavaScript in Selenium WebDriver? 如何为使用Javascript工具提示定义的Web元素查找Xpath? - How to find Xpath for a web element defined using a Javascript tooltip? 如何通过XPath从Selenium WebDriver中的JavaScript代码获取特定文本 - How to get a specific text from a javascript code in selenium webdriver by xpath 如何使用 JavaScript 单击 Selenium WebDriver 中的元素? - How to click an element in Selenium WebDriver using JavaScript? 如何使用javascript使用java使用selenium Webdriver设置所选Web元素的属性? - How to use javascript to set attribute of selected web element using selenium Webdriver using java? Selenium WebDriver无法使用Python通过Id查找元素 - Selenium WebDriver Unable to Find Element by Id, Using Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM