10 examples of 'javascript get element by xpath' in JavaScript

Every line of 'javascript get element by xpath' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
173export function clickElementByXPath(driver: WebDriver, xpath: string) {
174 return retry(5, driver, async function(driver, count) {
175 if (count>1 && config.LOG_DETAILS) console.log("clickElementByXPath ",xpath," attempt #",count);
176 let elem = await shadowRoot(driver);
177 elem = await findByXPath(elem, xpath);
178 await elem.click();
179 });
180 // Stale element possible:
181 // return to(driver.findElement(By.xpath(xpath)).click());
182}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

184export async function getTextByXPath(driver: WebDriver, xpath: string): Promise<string> {
185 return await retry(5, driver, async function(driver, count) {
186 if (count>1 && config.LOG_DETAILS) console.log("getTextByXPath ",xpath," attempt #",count);
187 let elem = await shadowRoot(driver);
188 elem = await findByXPath(elem, xpath);
189 return await elem.getText();
190 });
191}
145async function getTextByXPath(page, path) {
146 const elHandle = await page.waitForXPath(path);
147 return page.evaluate(el => el && el.textContent, elHandle);
148}
25function findElement(selectors) {
26 let currentElement = document;
27 for (let i = 0; i < selectors.length; i++) {
28 if (i > 0) {
29 currentElement = currentElement.shadowRoot;
30 }
31
32 currentElement = currentElement.querySelector(selectors[i]);
33
34 if (!currentElement) {
35 break;
36 }
37 }
38
39 return currentElement;
40}
212public async getTextFromElement(selector): Promise<string> {
213 return this.webdriverClient.getText(selector);
214}
16export function getElement(el: FinderResult): ElementFinder {
17 return typeof el === 'string' ? element(by.css(el)) : el;
18}
15public findElement(selector: string): WebElementPromise {
16 return this.driver.findElement(By.css(selector));
17}
2function getElementXPath(element) {
3 if (element && element.id)
4 return '//*[@id="' + element.id + '"]';
5 else
6 return getElementTreeXPath(element);
7}
29function getValue(selector) {
30 return browser.executeScript(`
31 return $('${selector}').val();
32 `);
33}
517function getFirstElementByXPath(xpath, node) {
518 var result = getXPathResult(xpath, node,
519 XPathResult.FIRST_ORDERED_NODE_TYPE || 9)
520 return result.singleNodeValue
521}

Related snippets