How to write xpath for the following HTML code

xpath examples
xpath // operator
xpath contains
xpath contains(text)
following-sibling xpath
xpath tester
xpath text
xpath parent

<p class=""> <b class="">Inventory:</b> 177<br><b class="">UNIT:(12 per unit) </b></p>

I need to get the value 177.

I tried with two xpaths the first one is

//*[@id='product_addtocart_form']/div[3]/p[2]/text(),it is throwing an exception invalid selector exception-it should be an element,

and another xpath is //p/b[1]/text() it is also throwing the same exception.

Try removing the /text() part of your xpath, and acquire the text, using SeleniumElement method getText() or an equivalent.

XPath in Selenium WebDriver: Complete Tutorial, XPath in Selenium WebDriver is used to find an element on the web XPath axes, Following, Ancestor, Child, Preceding, Following-sibling, You can start from the middle of the HTML DOM structure and no need to write long xpath. 49​) Execute JavaScript based code · 50) Using Selenium with Python  No need to use Xpath here if you could simply locate the element using By.id(). Asuming are using Java , you should try as below :- WebElement el = drive.findElement(By.id("compositePageTitleDiv")); String text = el.getText();

You can try to use this relative Xpath :

//*[@class='Your Class name']//*[text()='177']

XPath locator examples, After that, I recommend the XPath resources linked on our Types of Element in HTML, discounting the many ways to make something that looks like a button, but is not. If the button is a form submit button (declared with the <input> tag and Previous topic: Locating Elements: In an IFrame · Next topic: How to inspect an  XPath uses path expressions to select nodes or node-sets in an XML document. The node is selected by following a path or steps.

for the tags which you have given,

//p/b[contains(.,'Inventory')]/following-sibling::text()[normalize-space()]

the above Xpath works. But, for more accurate Xpath, share the page link to analyse the source code.

XPath Examples, selectNodes(xpath);. In our examples we have included code that should work with most major browsers. Select all the titles. The following example selects  Nov 10, 2003 · Part of the project is the ability to generate HTML pages on the fly from the course content - which means only showing a portion of the course at a time. to generate the email HTML body where you can write your HTML Code + use XPath How Do I Write Xpath In Chrome Browser.

Remove /text() from your xpath, and acquire the text, using SeleniumElement method getText() or an equivalent. It will give the text value of only html bold tag. In which html tag deos text 117 belong ?

XPath Tutorial, Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java  XPath is a syntax used to describe parts of an XML document. With XPath, you can refer to the first element, any attribute of the elements, all specific elements that contain the some text, and many other variations. An XSLT style-sheet uses XPath expressions in the match and select attributes of various elements to indicate how a document

Selecting content on a web page with XPath, What is XPath and how can I use it? By default, a web browser interprets the HTML code to determine what markup to apply to the scraper would need to follow at it navigates through the HTML content of the page to reach the title element. XPath can be used to navigate through elements and attributes in an XML document. XPath stands for XML Path Language. XPath uses "path like" syntax to identify and navigate nodes in an XML document. XPath contains over 200 built-in functions. XPath is a major element in the XSLT standard. XPath is a W3C recommendation.

Xpath cheatsheet, h1 + ul, //h1/following-sibling::ul[1] Xpath doesn't have the “check if part of space-separated list” operator, so this is //a/text() #=> "Go home" //a/@href #=> "index.html" //a/* #=> All a's child Use comparison and logic operators to make conditionals. Input tag cheatsheet · HTML meta tags cheatsheet · Layout thrashing  XPath is used to find the location of any element on a webpage using HTML DOM structure. The basic format of XPath is explained below with screen shot. Syntax for XPath: XPath contains the path of the element situated at the web page. Standard syntax for creating XPath is. Xpath=//tagname[@attribute='value'] // : Select current node.

How to write XPath under tag: strong<> <br> after every <strong , I am trying to find xpath of text 262567 of given below HTML code : <div class="​lessthanhalf" > <h4> You can use below xpath to get the text what you want to get using text node. You can use JavascriptExecutor to evaluate your xpath to get your desired text //div[@class='lessthanhalf']/strong[1]/following::text()[1].

Comments
  • The answer given by @Vignesh Waran locates the text exactly but I don't know why it's not working when I try to locate through selenium locator, it throws the error, So do onething, read the entire in <p> and then extract 177 from that. which language are you using? If you are using Ruby, I will update the answer to fetch that value.
  • @suvarnareddy As your HTML contains <p class="">, can you update the question a bit more of the outerHTML till we can identify the <p> tag uniquely?
  • Thank u @Edvins i tried like that but i am getting the result as "inventory:" only not that value ,is there any other way
  • it is throwing an invalid selector exception-it should be an element
  • This is amazing one, I have never written like this one, It perfectly works to locate the text 177 but when I tried to pass via selenium code, it throws the error that The expression is not a legal expression.