首页 > 解决方案 > 如何在字段 /selenium 中逐位输入数字

问题描述

所以我有包含 7 个字段的验证码,我有验证码本身。如何在每个字段中按数字输入此代码?我试着做List,但我的技能不是那么好。

标签: javaseleniumselenium-webdriverautomated-testspageobjects

解决方案


It is totally dependent on how you application looks and what the DOM structure is. I could help you with the following piece of code that shows how you can automate the verification code field. Execute the below code and see how it works.

            driver.get("https://codepen.io/DaniRC/pen/PowwwjZ");
            List<String> data = new ArrayList<String>();

            data.add("5");
            data.add("4");
            data.add("3");
            data.add("5");
            int counter = 0;

            driver.switchTo().frame("result");

            List<WebElement> elements = driver.findElement(By.id("form")).findElements(By.xpath("input[@type='text']"));

            for(WebElement element : elements)
            {
                element.sendKeys(data.get(counter));
                counter++;
            }
            Thread.sleep(10000);
            driver.close();

推荐阅读