如何获取WebElement的文本,然后在硒的其他字段中输入该文本?

时间:2019-05-13 20:39:35

标签: selenium selenium-webdriver

这是我需要执行的步骤:

1)获取字段文本。 (可以成功完成此操作,使用getText()方法并参考代码“ GetTextOfElement”)

2)将文本存储到字符串中。能够成功执行此操作,请参考控制台输出“我的复制值:A-W91QV1-19-0090”并参考代码“ GetTextOfElement”)

3)使用存储的字符串值到新字段。 (无法执行此操作,而是在步骤1中查找从中检索文本的元素,请参见代码“ enterTextInField”和控制台)

获取元素代码的文本:

// 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");
        }

这是完整的代码:

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");
}

要复制的字段值的屏幕快照: enter image description here 应将存储的字段值发送到的屏幕截图: enter image description here

这是控制台输出屏幕截图: enter image description here

1 个答案:

答案 0 :(得分:1)

这很简单,您离开了列出了应用程序ID的页面,因此硒不再可以从中获取文本了。

只需将值放在变量中,访问下一页,然后使用变量,就像这样:

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");
}

方法getTextOfElement()不会存储元素的文本,它会在每次调用该元素时搜索该元素并返回其文本,这就是为什么要在页面中显示该文本并将其放在变量,然后在想要的页面中使用变量(在这种情况下,该变量保存元素文本)。