如何使用 Selenium 中的 Actions 在编辑框中输入大写字母?
software testingautomation testingselenium web driver更新于 2026/2/10 14:37:17
我们可以借助 Actions 在 Selenium 的编辑框中输入大写字母。为此,我们需要先移动到编辑框,然后在该字段上单击 (click())。然后按住 SHIFT 键并使用 sendKeys() 方法输入字母。最后使用 build().perform() 执行所有步骤。
示例
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;
import java.util.concurrent.TimeUnit;
public class UpperCase{
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
WebDriver driver = new ChromeDriver();
String url = "https://www.tutorialspoint.com/questions/index.php";
driver.get(url);
driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS);
// 移动到元素,按下 shift 然后输入字母,然后 //perform() 执行 操作
Actions a = new Actions(driver);
a.moveToElement(driver.findElement(By.xpath(“input[@type=’text’]))).
click().keyDown(Keys.SHIFT).sendKeys(“tutorialspoint”).
build().perform();
driver.quit();
}
}

