-1

Suppose I have following code:

<div class="Class">
<h3>First Title H3</h3>
    First Description <br />
    Choose Option:
    <select id="Id" name="Name">
       <option value="Value1">Option1 $Price1</option>
       <option value="Value2">Option2 $Price2</option>
       <option value="Value3">Option3 $Price3</option>
    </select>
<h3>Second Title H3</h3>
    Second Description <a href="https://www.example.com/">Link</a>
</div>
  • What's the Xpath to print "First Description" only?
  • What's the XPath to print "$Price1" only (without/exclude "Option1")? Thanks for your help.
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
panjianom
  • 236
  • 4
  • 18

3 Answers3

2

I'm not sure what is your desired output, but here is what should help you.

following-sibling::text() would help you to check the First Description:

//div/h3[contains(following-sibling::text(), "First Description")]

For the first option of the select tag, I would rely on the select's id attribute and option's value attribute:

//select[@id="Id"]/option[@value="Value1"]/text()
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
0

Solved based on this answers: using XPath: how to exclude text in nested elements and XPath: select text node and Get second element text with XPath?

Community
  • 1
  • 1
panjianom
  • 236
  • 4
  • 18
0

String value = driver.findElement(By.xpath("//*[@value ='Value1']").getText();

String strarr[] = value.split(" ");

System.out.println("Price1 :"+ strarr[1]);

I think this will work, we are finding the text and splitting with space.