-1
**Element Page**
public class BasePage {
  @FindBy(className = "_42ft _4jy0 _52e0 _4jy6 _4jy1 selected _51sy")//Signin Button
    private WebElement SigninButton;
    public void clickOnSigninButton() {
        SigninButton.click();
    }

    @FindBy(className= "pam _3-95 _9ay3 uiBoxRed")//Get Username erroe msg
    private WebElement geterrormsg;
    public String ClickonAdmissionnumber() {
        geterrormsg.getText();
    }
}
**Another Class Test case page**
public class Tc_1 {
public void  LoginTC1() throws Exception
{       
**//create page objects**
  BasePage lp = new BasePage();     
**//invoke the methods**
  lp.clickOnSigninButton();
  lp.Errormsg();
**//write actual data to excel**
ExcelLibrary.writeData("Sheet1", 1, 5, Actualmsg);
}    

Trying to fetch text from this class attribute? I have tried with get attribute value but not able to fetch data. Please help me out.... text output "Wrong credentials Invalid username & Password".

enter image description here

  • Please elaborate what does mean "not able to fetch data". Update your question with URL or page source code and stacktrace. – pburgr Dec 10 '21 at 07:40
  • Btw don't use first letter capital for naming objects. See https://www.javatpoint.com/java-naming-conventions – pburgr Dec 10 '21 at 07:42
  • 1
    I suggest you to accept given you answers. This may increase your chances to get a good answers... – Prophet Dec 10 '21 at 07:45
  • @pburgr I have updated kindly check it once – Saikumar Reddy Yelampalli Dec 10 '21 at 07:50
  • In my case the login page has differrent looks and 'wrong credentials' text is between password input and the login button. What browser are you using for the test? – pburgr Dec 10 '21 at 07:58
  • @pburgr I have updated with actual code. Using Chrome version 96.0.4664.93 So actually what i am doing is Without giving any inputs Cred. directly clicking on login button. error msg is displayed so i need to get that error message and past it in excel sheet. – Saikumar Reddy Yelampalli Dec 10 '21 at 08:31
  • Even witout entering any credentials the error message looks like this https://pasteboard.co/WB3nV81l54xN.png. What exact URL are you entering? – pburgr Dec 10 '21 at 08:47
  • @pburgr https://www.facebook.com/login/?privacy_mutation_token=eyJ0eXBlIjowLCJjcmVhdGlvbl90aW1lIjoxNjM5MTI2ODUyLCJjYWxsc2l0ZV9pZCI6MzgxMjI5MDc5NTc1OTQ2fQ%3D%3D – Saikumar Reddy Yelampalli Dec 10 '21 at 09:01
  • Still on the same screen I've posted. – pburgr Dec 10 '21 at 09:03
  • @pburgr okay consider this page only https://pasteboard.co/WB3nV81l54xN.png Try to get that text (https://pasteboard.co/0ib38vTRfwsC.png) and past it in excel file – Saikumar Reddy Yelampalli Dec 10 '21 at 09:06

2 Answers2

0

Code:

package selenium;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

public class SaikumarFbTest extends WebDriverSetup {

    public static void main(String[] args) {
        
        // wrapped driver initialization
        WebDriver driver = startChromeDriver();
        
        //entering url
        driver.get("https://www.facebook.com/login");
        
        // accept cookie consent in CZ
        driver.findElement(By.xpath("//button[contains(@data-testid,'cookie-policy-dialog-accept-button')]")).click();  
        
        // switch to english using link in footer
        driver.findElement(By.xpath("//a[contains(@href,'https://www.facebook.com/login')]")).click();
        
        //accept cookie consent in EN
        driver.findElement(By.xpath("//button[contains(@data-testid,'cookie-policy-dialog-accept-button')]")).click();  

        // click login button
        driver.findElement(By.id("loginbutton")).click();
        
        // get <div class="clearfix _5466 _44mg" id="email_container"></div>
        WebElement emailContainer = driver.findElement(By.id("email_container"));
        
        // get child divs
        List<WebElement> emailContainerChildDivs = emailContainer.findElements(By.tagName("div"));
        
        // print error text
        System.out.println(emailContainerChildDivs.get(1).getText());
        driver.quit();
    }
    
}

Output:

Starting ChromeDriver 96.0.4664.45 (76e4c1bb2ab4671b8beba3444e61c0f17584b2fc-refs/branch-heads/4664@{#947}) on port 23066
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
Pro 10, 2021 11:30:39 DOP. org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
The email or mobile number you entered isn’t connected to an account. Find your account and log in.

To save the text to XLSX file, use Apache POI -> https://www.codejava.net/coding/how-to-write-excel-files-in-java-using-apache-poi.

pburgr
  • 1,722
  • 1
  • 11
  • 26
0
**Element Page**
public class BasePage {
  @FindBy(className = "_42ft _4jy0 _52e0 _4jy6 _4jy1 selected _51sy")//Signin Button
    private WebElement SigninButton;
    public void clickOnSigninButton() {
        SigninButton.click();
    }

    @FindBy(className= "pam _3-95 _9ay3 uiBoxRed")//Get Username erroe msg
    private WebElement geterrormsg;
    public String ClickonAdmissionnumber() {
       return geterrormsg.getText();
    }
}
**Another Class Test case page**
public class Tc_1 {
public void  LoginTC1() throws Exception
{       
**//create page objects**
  BasePage lp = new BasePage();     
**//invoke the methods**
  lp.clickOnSigninButton();
  String Actualmsg = lp.Errormsg();
**//write actual data to excel**
ExcelLibrary.writeData("Sheet1", 1, 5, Actualmsg);
}