Selenium, unable to obtain element

advertisements

i need to read the text from a element of a webpage via selenium. Here's the part of the page:

<div id="pDFEA208F010FEDCF6C6F0D5B5DDC97F3154" class="NPC_collapsable NPC_widget NPC_complete NPC_boxselect" data-boxtype="TOGGLE_BUTTONS" data-collapsed="true" style="margin-top: 20px;">
  <header class="NPC_widget-header NPC_collapsing-title NPC_statusIcon">
    <h3 class="NPC_parameterText">Baugröße</h3>
    <span class="NPC_ellipsis NPC_unit"></span>
    <span class="NPC_ellipsis NPC_statusIcon" style="color:#0091dc!important"></span>
    <span class="NPC_ellipsis NPC_selectedParameterValue" style="font-weight: bold;">6</span>
    <span class="NPC_hint"></span>
  </header>

I can "adress" the section of the page via the <h3>Baugröße</h3>, what i need to get is the 6 from the last but one span. I have:

    WebElement gruppe = driver.findElement(By.xpath("//header/h3[text()='" + propertyName + "']"));
    System.out.println("Group: " + gruppe.getText() + " <");

    // Element in den sichtbaren Bereich(Focus) der Seite bewegen
    ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", gruppe);

    WebElement t1 = driver.findElement(By.xpath("//header/h3[text()='" + propertyName + "']/../span[@class='NPC_ellipsis NPC_selectedParameterValue']"));
    System.out.println("Try 1: >" + t1.getText() + "<");

    WebElement set = gruppe.findElement(
            By.xpath(".//following-sibling::*/span[@class='NPC_ellipsis NPC_selectedParameterValue']"));
    System.out.println("set: >" + set.getText() + "<");

Which gives me the group "Baugröße", but not the "6". I get:

Group: Baugröße <
Try 1: ><
org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":".//following-sibling::*/span[@class='NPC_ellipsis NPC_selectedParameterValue']"} Command duration or timeout: 68 milliseconds

How can i get this span element?


gruppe this is object of h3 and when you try to find element locator using gruppe it will search element as a child of tag h3 and there is no child of H3 so it is giving exception element not found.

Now to get value 6 you should use driver object like below code:

 WebElement set = driver.findElement(
        By.xpath("//span[contains(@class, 'NPC_ellipsis NPC_selectedParameterValue')]"));
 System.out.println("set: >" + set.getText() + "<");