Annie
Annie

Reputation: 25

Using selenium, how to select a the text in a paragraph which is nested in a div element?

Sample code:

<div class="loginbox">some code</div>
<div class="loginbox">other code</div>
<div class="loginbox">
    <p> style="color: Red;">Test Extract</p>     
</div>

Using Selenium Web Driver, I would like to extract the text Test Extract within the paragraph element which is nested within a div, whose class name is shared with other div classes. c# preferred.

Upvotes: 1

Views: 3635

Answers (2)

Dinesh Kumar
Dinesh Kumar

Reputation: 81

C# code to get the text from the locator specified,

IWebElement element = Browser.GetElementByCssSelector("div.loginbox p");
string text = element.Text;

Upvotes: 0

Harish
Harish

Reputation: 306

You can try below method:

    driver.findElement(By.xpath("//div[@class='loginbox']/p")).getText();

EDITED You should use = inside the square braces like:

driver.findElement(By.xpath("//div[@class='loginbox']/p");

Upvotes: 3

Related Questions