-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.
0

3 Answers 3

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()
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your answer, alecxe. I will try it. Sorry for my English language, I updated the question.
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?

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.