Pankaj Sharma
Pankaj Sharma

Reputation: 163

finding element by classname and tagname using selenium webdriver

There are classes and tagname and I am writing the below selenium code to find description from below code but its not working.

WebElement WWdescription = driver.findElement(By.className("atb-delivery-accordions").className("section highlight-01").tagName("p"));    


<div class="atb-delivery-accordions">
    <div class="page-accordion opened">
    <input id="moreDetails-acc" class="acc-check" type="checkbox" checked="">
    <label class="acc-label opened" data-panel-id="moreDetailsAcc" for="moreDetails-acc">Description</label>
    <div class="content" data-panel-id="moreDetailsAcc" style="display: block;">
    <div class="information-panel">
    <div class="subcontent">
    <div class="section highlight-01">
    <p>A pretty floral lace collection combining contrast bows and trims for a feminine on trend look. The fuller coverage of our post surgery bras provide support, comfort and confidence. The dual cotton pockets are perfect for a prosthesis. Combine style and value with this pack of 2 bras.</p>

Upvotes: 8

Views: 20530

Answers (3)

Kiran Angara
Kiran Angara

Reputation: 11

It is better to use XPath or CSS selector instead of other locaters i.e., className, id, name or tagName. I also experienced the same i.e., when I tried with XPath I was able to locate the element properly and was able to click on it.

Upvotes: 1

Vishal Jagtap
Vishal Jagtap

Reputation: 896

Try below:-

WebElement description = driver.findElement(By.xpath("//div[@class='atb-delivery-accordions']/p"));

String descriptionText=description.getText();

Upvotes: 0

Saritha G
Saritha G

Reputation: 2608

Try something like this:

 WebElement parentEle = driver.findElement(By.xpath("//div[@class='atb-delivery-accordions']"));

 WebElement descriptionEle = parentEle.findElement(By.tagName("p"));

 //Get the text from the description element.
 String descriptionText = descriptionEle.getText();

Upvotes: 6

Related Questions