How to Get Element by Xpath Using JavaScript in Selenium Webdriver

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

You can use document.evaluate:

Evaluates an XPath expression string and returns a result of the
specified type if possible.

It is w3-standardized and whole documented: https://developer.mozilla.org/en-US/docs/Web/API/Document.evaluate

function getElementByXpath(path) {  return document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;}
console.log( getElementByXpath("//html[1]/body[1]/div[1]") );
<div>foo</div>

webelement selenium - how to found an element with xpath and link with onclick

Please use the below xpath :

//a[contains(@onclick,'submitData('updateShortcodeAdmin')')]

Explanation :

You are using = in contains method, it should have been ,

I checked in compiler that

webDriver.findElement(By.xpath("//a[contains(@onclick,'submitData('updateShortcodeAdmin')')]"));

does not show any compile error.

PS : Please check in the dev tools (Google chrome) if we have unique entry in HTML DOM or not.

Steps to check:

Press F12 in Chrome -> go to element section -> do a CTRL + F -> then paste the xpath and see, if your desired element is getting highlighted with 1/1 matching node.

Update :

webDriver.findElement(By.xpath("//a[contains(@onclick,'updateShortcodeAdmin')]"));

Cannot find element by xpath (JS activated website Selenium chrome)

The element is inside iframe.You need to switch it first to access the element.

from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait

site = 'https://finra-markets.morningstar.com/BondCenter/BondDetail.jsp?ticker=C614515&symbol=BBBY4144685'
browser = webdriver.Chrome(executable_path)
browser.get(site)
WebDriverWait(browser,10).until(EC.frame_to_be_available_and_switch_to_it((By.ID,"ms-bond-detail-iframe")))
print(browser.find_element_by_xpath('//*[@id="msqt_summary"]/div[2]/table/tbody/tr[1]/td[2]/span[@class="gr_text1"]').text)

To come out from iframe you need to use.

browser.switch_to.default_content()

Get text from XPath Element using Selenium WebDriver with JavaScript

Use the following xpath to identify the button.

//div[@class='purchase-info']//input[@value='Add to Basket']

Update:

const value = await driver.findElement(web.By.xpath("//div[@class='purchase-info']//input[@value='Add to Basket']")).getAttribute("value");
console.log(value)

Get text from XPath located Element using Selenium WebDriver with JavaScript

The function you want is getText().

String text = driver.findElement(webdriver.By.xpath("//div/span")).getText();

How do I get to P tag in the below HTML using Xpath ? Thanks

It is alway good to look for elements with a @id-attribute. They should be unique and the XPath engine will probably find those the fastest. So that is oaky.

Since I assume that the order and number of divs vary and the presented sections don't seem to close in this example, I would use this XPath:

//*[@id="page"]//p[text()='Sorry, you do not have access the to the requested page']


Related Topics



Leave a reply



Submit