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
Display Date/Time in User's Locale Format and Time Offset
How to Order Events Bound with Jquery
Difference Between 'Return Await Promise' and 'Return Promise'
Can You Get a Users Local Lan Ip Address via JavaScript
Promise - How to Force Cancel a Promise
Get the Closest Number Out of an Array
How to Escape a JSON String Containing Newline Characters Using JavaScript
Javascript: Setting Location.Href Versus Location
Primitive Value VS Reference Value
What Does a Tilde Do When It Precedes an Expression
How Does JavaScript's Sort() Work
JavaScript Regex - Look Behind Alternative
Call ASP.NET Function from JavaScript
How to Send a Variable Number of Arguments to a JavaScript Function
What Values Can a Constructor Return to Avoid Returning This
Why Doesn't My Equality Comparison Using = (A Single Equals) Work Correctly