Haider T
Haider T

Reputation: 77

How do I Get Text of a WebElement and then enter that Text In a different Field in selenium?

Here are the Steps I need to perform:

1) Get text of the field. (Able to do this successfully, Using getText() method and Refer to code "GetTextOfElement")

2) Store the text to a String. Able to do this sucessfully, Refer to console output My copied value: A-W91QV1-19-0090 and Refer to Code "GetTextOfElement")

3) Use stored String value to a new field. (Not able to do this, Instead it is looking for the element where the text was retrieved from in step 1, Refer to code "enterTextInField" and console)

GET TEXT OF Element Code:

// Get Text of Element to store in variable call "text"
        public String getTextOfElement() throws Exception {
            WebDriverWait wait = new WebDriverWait(driver, 30);
            wait.until(ExpectedConditions.visibilityOfElementLocated(Application_ID));
             String text = driver.findElement(Application_ID).getText();
            driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
            System.out.println("My copied value: " + text);
            // return elementText;
            return text;
        }

Enter Text In Field code:

// Enter Text of element from stored variable from getTextOfElement()
        public void enterTextInField() throws Exception {
            driver.findElement(solicitation.Identifier_Field).clear();
            driver.findElement(solicitation.Identifier_Field).sendKeys(getTextOfElement());
             System.out.println("value copied");
        }

Here is the entire code:

public void createNewWarrantPage() throws Exception {

    /********************************************************************************************************************************
     * Initiate Warrant Application
     ********************************************************************************************************************************/
    WebDriverWait wait = new WebDriverWait(driver, 40);

      // Navigating to the Warrant Page
      driver.findElement(Transaction_Link).click();
      driver.findElement(Acquisitions_Link).click();
      driver.findElement(Additional_Form_Link).click();
      driver.findElement(New_Link).click();
      driver.findElement(Warrent_link).click(); 
    // switching to page Iframe
      WebElement iframe = driver.findElement(By.xpath("//*[@id='PegaGadgetIfr']"));
      driver.switchTo().frame(iframe); // Filling out all data's for the page
      driver.findElement(Warrent_Template_Field).sendKeys("CLASS_I");
      //Sending the newly created KO User to Canidate ID field
      synchoWait();
      driver.findElement(By.id("CandidateOpID")).sendKeys(Keys.chord(Keys.CONTROL, "v"));
      synchoWait();
      driver.findElement(By.xpath("//*[@id='po0']")).click();
      synchoWait(); 
      driver.findElement(DoDDAC_Input_Field).sendKeys("W91QV1");
      driver.findElement(PCO_CheckBox).click(); synchoWait();
      driver.findElement(Limited_Radio_Button).click();
      driver.findElement(Prejudice_radio_Button).click();
      synchoWait();
      driver.findElement(Semester_radio_Button).click();
      synchoWait();
      driver.findElement(Supervisor_Field).sendKeys("dschrute"); 
      synchoWait();
      driver.findElement(New_Warrant_Submit_Button).click();
       synchoWait();
       getTextOfElement();
      driver.switchTo().defaultContent();
      driver.findElement(Transaction_Link).click();
      driver.findElement(Acquisitions_Link).click();
      synchoWait();
      solicitation = new Solicitation();
      driver.findElement(solicitation.Contract_File).click();
      enterTextInField();

      driver.findElement(solicitation.Search_Button).click();


}

// Get Text of Element to store in variable call "text"
public String getTextOfElement() throws Exception {
    WebDriverWait wait = new WebDriverWait(driver, 30);
    wait.until(ExpectedConditions.visibilityOfElementLocated(Application_ID));
     String text = driver.findElement(Application_ID).getText();
    driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
    System.out.println("My copied value: " + text);
    // return elementText;
    return text;
}

// Enter Text of element from stored variable from getTextOfElement()
public void enterTextInField() throws Exception {
    driver.findElement(solicitation.Identifier_Field).clear();
    driver.findElement(solicitation.Identifier_Field).sendKeys(getTextOfElement());
     System.out.println("value copied");
}

Screen shot of Field value of being copied from: enter image description here Screen shot of where stored field value should be sent to: enter image description here

Here is the console output screenshot: enter image description here

Upvotes: 0

Views: 498

Answers (1)

Valga
Valga

Reputation: 459

It's pretty simple, you left the page where the Application ID was listed, so selenium can't find it anymore to get the text from it.

Just put the value in a variable, access the next page, and use the variable, like this:

public void createNewWarrantPage() throws Exception {

/********************************************************************************************************************************
 * Initiate Warrant Application
 ********************************************************************************************************************************/
WebDriverWait wait = new WebDriverWait(driver, 40);

  // Navigating to the Warrant Page
  driver.findElement(Transaction_Link).click();
  driver.findElement(Acquisitions_Link).click();
  driver.findElement(Additional_Form_Link).click();
  driver.findElement(New_Link).click();
  driver.findElement(Warrent_link).click(); 
// switching to page Iframe
  WebElement iframe = driver.findElement(By.xpath("//*[@id='PegaGadgetIfr']"));
  driver.switchTo().frame(iframe); // Filling out all data's for the page
  driver.findElement(Warrent_Template_Field).sendKeys("CLASS_I");
  //Sending the newly created KO User to Canidate ID field
  synchoWait();
  driver.findElement(By.id("CandidateOpID")).sendKeys(Keys.chord(Keys.CONTROL, "v"));
  synchoWait();
  driver.findElement(By.xpath("//*[@id='po0']")).click();
  synchoWait(); 
  driver.findElement(DoDDAC_Input_Field).sendKeys("W91QV1");
  driver.findElement(PCO_CheckBox).click(); synchoWait();
  driver.findElement(Limited_Radio_Button).click();
  driver.findElement(Prejudice_radio_Button).click();
  synchoWait();
  driver.findElement(Semester_radio_Button).click();
  synchoWait();
  driver.findElement(Supervisor_Field).sendKeys("dschrute"); 
  synchoWait();
  driver.findElement(New_Warrant_Submit_Button).click();
   synchoWait();
   String applicationID = getTextOfElement();
  driver.switchTo().defaultContent();
  driver.findElement(Transaction_Link).click();
  driver.findElement(Acquisitions_Link).click();
  synchoWait();
  solicitation = new Solicitation();
  driver.findElement(solicitation.Contract_File).click();
  enterTextInField(applicationID);

  driver.findElement(solicitation.Search_Button).click();


}

// Get Text of Element to store in variable call "text"
public String getTextOfElement() throws Exception {
    WebDriverWait wait = new WebDriverWait(driver, 30);
    wait.until(ExpectedConditions.visibilityOfElementLocated(Application_ID));
     String text = driver.findElement(Application_ID).getText();
    driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
    System.out.println("My copied value: " + text);
    // return elementText;
    return text;
}

// Enter Text of element from stored variable from getTextOfElement()
public void enterTextInField(String value) throws Exception {
    driver.findElement(solicitation.Identifier_Field).clear();
    driver.findElement(solicitation.Identifier_Field).sendKeys(value);
     System.out.println("value copied");
}

The method getTextOfElement() DOES NOT store the text of the element, it searches for the element and returns its text everytime you call it, that's why you should get the text while in the page it's being displayed and put it in a variable, then use the variable (that holds the element text in this case) in the page you want to.

Upvotes: 1

Related Questions