-3

I need to get and print the Class name class="community-wrapper bg-blue-dark" for the text "Selenium Java" like wise for Selenium Webdriver, Selenium Demo and Selenium Learning.

String classname = driver.findElement(By.xpath("//*contains[@text() = "Selenium Java")).getAttribute("class");

System.out.println(classname);
  1. Help needed to print all the Class names preceding to the text Selenium Java in console.
  2. Help need to print specific class name class="community-wrapper bg-blue-dark" in console.

HTML:

<div class="test-content test-GridColumn test-GridColumn--default--12">
   <div class="community-wrapper bg-blue-dark">
  <div class="row align-center small-align-center large-px4 medium-py5 px1 py2 large-py4">
     <div class=" columns large-10 xlarge-8 medium-11 small-12">
        <h3 data-aos="fade-up" data-aos-delay="0" class="text-white aos-init aos-animate" style="font-weight:300;">
           <p><i>Selenium Java</i></p>
        </h3>
        <h6 data-aos="fade-up" data-aos-delay="100" class="text-white text-bold aos-init">
           Selenium Webdriver
        </h6>
        <h5 data-aos="fade-up" data-aos-delay="200" class="text-white text-bold aos-init">
           Selenium Demo
        </h5>
        <p data-aos="fade-up" data-aos-delay="300" class="text-white aos-init">
           Selenium Learning
        </p>
     </div>
  </div>
   </div>
</div>
NarendraR
  • 7,577
  • 10
  • 44
  • 82

2 Answers2

0

Use below code :

List<WebElement> itemList = driver.findElements(By.xpath("//div/*[@data-aos='fade-up']"));
for (WebElement element : itemList) {
        if (element.getText().contains("Selenium Java")) {
            System.out.println("Selenium Java" + element.getAttribute("class"));
        } else if (element.getText().contains("Selenium Webdriver")) {
            System.out.println("Selenium Webdriver" + element.getAttribute("class"));
        } else if (element.getText().contains("Selenium Demo")) {
            System.out.println("Selenium Demo" + element.getAttribute("class"));
        } else if (element.getText().contains("Selenium Learning")) {
            System.out.println("Selenium Learning" + element.getAttribute("class"));
        } else {
            System.out.println("not expected label" + element.getText());
        }
}
NarendraR
  • 7,577
  • 10
  • 44
  • 82
0

To print the value of the class attribute with respect to the texts Selenium Webdriver, Selenium Demo and Selenium Learning you need to induce WebDriverWait for the visibilityOfElementLocated() and you can use the following based Locator Strategies:

  • Using the text Selenium Java:

    System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//i[text()='Selenium Java']//ancestor::div[3]"))).getAttribute("class"));
    
  • Using the text Selenium Webdriver:

    System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//h6[normalize-space()='Selenium Webdriver']//ancestor::div[3]"))).getAttribute("class"));
    
  • Using the text Selenium Demo:

    System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//h5[normalize-space()='Selenium Demo']//ancestor::div[3]"))).getAttribute("class"));
    
  • Using the text Selenium Learning:

    System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//p[normalize-space()='Selenium Learning']//ancestor::div[3]"))).getAttribute("class"));
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352