Is there a way to get element by XPath using JavaScript in Selenium WebDriver?

Is there a way to get element by XPath using JavaScript in Selenium WebDriver?

WebElements can be located with selenium webdriver using XPaths. These are expressions that identify elements on a page based on certain details, such as, if the element contains a specific attribute or has a specific child node.

const query = await driver.findElement(By.xpath("//*[contains(text(), 'Wikipedia')]"))

To explain what the different parts of the xpath in the above statement do:


  • // means to select an element descending from the root element

  • * means the element can be of any type

  • @class means to select an element matching the class attribute of the WebElement.


Using Java

Using Javascript

Using Python

Leave a reply

Your email address will not be published. Required fields are marked*

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.