Search code examples
javaseleniumselenium-webdriverautomation

Extract Text from Website without any HTML tags with selenium


enter image description here

Not able to extract the highlighted text alone as a string with selenium since those two are hardcoded texts without tags, kindly let me know any possiblilities.


Solution

  • You can use the below xpath:

    //strong[text()='Customer']/../following-sibling::div/descendant::div[starts-with(@class,'col-md-10')]
    

    like this:

    String gottenText = new WebDriverWait(driver, Duration.ofSeconds(20)).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//strong[text()='Customer']/../following-sibling::div/descendant::div[@class='col-md-10']"))).getAttribute("innerText");
    System.out.println(gottenText);
    

    that shall provide the below output:

    Email user@phptravels.com
    Password demouser
    

    Update:

    you can store them into String variable like below:

    String gottenText = new WebDriverWait(driver, Duration.ofSeconds(20)).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//strong[text()='Customer']/../following-sibling::div/descendant::div[@class='col-md-10']"))).getAttribute("innerText");
    String[] arr = gottenText.split(" ");
    String[] userNames = arr[1].split("\\r?\\n");
    String userName = userNames[0];
    String password = arr[2];
        
    System.out.println(userName + " " + password);