美文网首页
seleniumm java 中点击发送 tab 键

seleniumm java 中点击发送 tab 键

作者: ambition_timid | 来源:发表于2022-09-06 15:35 被阅读0次
    package com.example.demo;
    
    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.Actions;
    
    import java.awt.*;
    import java.awt.event.KeyEvent;
    
    public class Test {
        public static void main(String[] args) throws Exception {
            System.setProperty("webdriver.chrome.driver", "D:\\chromedriver\\chromedriver.exe");
            WebDriver driver = new ChromeDriver();
            driver.get("https://www.google.com/");
            driver.manage().window();
            WebElement input = driver.findElement(By.xpath("/html/body/div[1]/div[3]/form/div[1]/div[1]/div[1]/div/div[2]/input"));
    
            // 输入框输入内容
            input.sendKeys("seleniumm");
    
            // 清除内容
            Robot robot = new Robot();
            robot.keyPress(KeyEvent.VK_TAB);
            robot.keyPress(KeyEvent.VK_ENTER);
    
    
            // F5按键刷新页面
            Thread.sleep(2000);
            robot.keyPress(KeyEvent.VK_F5);
            driver.quit();
        }
    }
    

    搜了很多不生效,只有使用 Robot 会生效

    相关文章

      网友评论

          本文标题:seleniumm java 中点击发送 tab 键

          本文链接:https://www.haomeiwen.com/subject/dbugnrtx.html