• Selenium Video Tutorials

Selenium - 文字框互動



在本節中,我們將瞭解如何與文字框互動。我們可以使用“sendkeys”方法將值放入文字框中。類似地,我們還可以使用 getattribute("value") 命令從文字框中檢索文字。請檢視以下示例。

selenium_ide_177

示例

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;

public class webdriverdemo {
   public static void main(String[] args) throws InterruptedException {
   
      WebDriver driver = new FirefoxDriver();
      // Puts a Implicit wait, Will wait for 10 seconds before throwing exception
      driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
      
      // Launch website
      driver.navigate().to("http://www.calculator.net/percent-calculator.html");
      
      // Maximize the browser
      driver.manage().window().maximize();
      
      // Enter value 10 in the first number of the percent Calculator
      driver.findElement(By.id("cpar1")).sendKeys("10");
      
      Thread.sleep(5000);
      
      // Get the text box from the application
      String result = driver.findElement(By.id("cpar1")).getAttribute("value");
      
      // Print a Log In message to the screen
      System.out.println(" The Result is " + result);
      
      // Close the Browser.
      driver.close();
   }
}

輸出

上面指令碼的輸出顯示如下。

selenium_ide_183
selenium_user_interactions.htm
廣告

© . All rights reserved.