简体   繁体   中英

How can I enter number digit by digit in field /selenium

So I have verification code that contains 7 fields and I have verification code itself. How can I enter this code by number in every field? I tried to do List, but my skills are not that good.

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();

I suggest you to not use a list. For every field you should use another function. Because in time, devs will delete or rename or move.. any field and your test will failed because they do a simple action with just one of field. So I think you should use one function for each field.

typeDigital(String number){ driver.findElemeny(By.css("css"))).sendKeys(number));}

here you will set your number from parapeter. If you have more question about this write me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM