user1603518
user1603518

Reputation: 41

How do I get this div tag using selenium webdriver?

<div id="ctl00_ContentHolder_vs_ValidationSummary" class="errorblock">
    <p><strong>The following errors were found:</strong></p>
    <ul><input type="hidden" Name="SummaryErrorCmsIds" Value="E024|E012|E014" />
        <li>Please select a title.</li>
        <li>Please key in your first name.</li>
        <li>Please key in your last name.</li>
    </ul>
</div>

I want to capture the text having value of E024 E012 and E014 and write it in to an Excel file.

I tried the following but it doesn't work.

string val1 = driver.FindElement(By.XPath("//div[contains(@class, 'errorblock'/ value = 'E024|E012|E014'")).Text;

How can I do this?

Upvotes: 1

Views: 13401

Answers (6)

Sonu
Sonu

Reputation: 139

IN C# it should be as follows.

String var1= driver.FindElement(By.XPath("//div[contains(@id, 'ValidationSummary')]")).Text;

Upvotes: 0

Madhu
Madhu

Reputation: 497

Better way is to use XPATH

 driver.FindElement(By.XPath("...\li")).Text; (OR)

 driver.FindElement(By.XPath("...\li")).GetAttribute("value"); 

Upvotes: 1

Frigik
Frigik

Reputation: 449

Try this:

    var div = driver.FindElement(By.XPath("//div[contains(@id, 'ValidationSummary')]"));
    var targetElement = div.FindElement(By.XPath("//ul[contains(@name, 'SummaryErrorCmsIds')]"));
// or var targetElement = div.FindElement(By.TagName("input"))

And then get info you need from this element.

Upvotes: 0

JacekM
JacekM

Reputation: 4099

I am not sure if this is what you meant but my example solution would be:

WebElement element = driver.findElement(By.className("errorblock")).findElement(By.tagName("ul"));
List<WebElement> values = element.findElements(By.tagName("li"))

for (WebElement value : values)
{
   String valueString = value.getText();
   //write to excel or whatever
}

Upvotes: 0

some_other_guy
some_other_guy

Reputation: 3394

In java -

String x = driver.findElement(By.name("SummaryErrorCmsIds")).getAttribute("Value"));

Upvotes: 1

Greg
Greg

Reputation: 5588

Your problem is you're selecting the wrong elements. I have no idea what your html looks like but try looking into FindElements and try doing a lookup by 'li'

Take a look here: Using Selenium 2's IWebDriver to interact with elements on the page

Upvotes: 0

Related Questions