JAVA使用webdriver模拟登录

 public static void main(String[] args) throws MalformedURLException {
        /**
         * 1. 引入Jar包
         * 报 Exception in thread "main" java.lang.NoClassDefFoundError: org/openqa/selenium/internal/Require
         * 是由于版本的问题
         *         <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
         *         <dependency>
         *             <groupId>org.seleniumhq.selenium</groupId>
         *             <artifactId>selenium-java</artifactId>
         *             <version>3.141.59</version>
         *         </dependency>
         * 2.引入驱动  https://www.selenium.dev/zh-cn/documentation/overview/
         */
        System.setProperty("webdriver.gecko.driver","D:\\java_project\\springBootStudy\\src\\main\\resources\\geckodriver.exe");
        FirefoxOptions options = new FirefoxOptions();

        FirefoxDriver driver = new FirefoxDriver(options);
        
        // 设置开启浏览器的超时间,保证DOM元素加载完成
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);

        driver.get("https://tpass.shanghai.chinatax.gov.cn:8443/#/login?redirect_uri=https%3A%2F%2Fetax.shanghai.chinatax.gov.cn%2Flogin-web%2Fapi%2Fthird%2Fsso%2Flogin%2Fredirect%3Fqd%3DKEXIN%26channelId%3Dweb%26goto%3D30010666&client_id=d598efbeddc7558c98fc32197114a36b&response_type=code&state=pro");

        List<WebElement> inputElements = driver.findElementsByCssSelector("input[class='el-input__inner']");
        
        // 设置值
        inputElements.get(0).sendKeys("9131011");
        inputElements.get(1).sendKeys("18005173334");
        inputElements.get(2).sendKeys("yu@njipen");

        WebElement handlerElement = driver.findElementByCssSelector(".handler");
        Point location = handlerElement.getLocation();
        System.out.println(location.toString());
        Actions actions = new Actions(driver);
        // 滑动验证
        actions.clickAndHold(handlerElement).moveByOffset(367,0).perform();
        actions.release();

        WebElement buttonElement = driver.findElementByCssSelector("button.loginCls");
        buttonElement.click();
        System.out.println(driver.toString());
    }

 

posted @ 2022-08-26 17:16  lujiacheng-python  阅读(567)  评论(0编辑  收藏  举报