How to get a random element in code in JAVA?

2019-06-14 19:51发布

I would like to get a random element on my code. For example i have 4 elements on my code in JAVA. I trying to get second element and start run program on this place. Another time when I start my program would be a one element. How do i get this?

        first element :

    while ( true ) {

         driver.get( "example_of_page" );
            WebElement formEmail = driver.findElement(By.name("email"));
            formEmail.sendKeys(Email1);
            WebElement formPassword = driver.findElement(By.name("pass"));
        formPassword.sendKeys(Password2);
        WebElement formRemember = driver.findElement(By.name("persistent"));
        formRemember.sendKeys(" ");
        formPassword.submit();

        second element :


        driver.get("example_of_page");
        WebElement formEmail = driver.findElement(By.name("email"));
        formEmail.sendKeys(Email2);
        WebElement formPassword = driver.findElement(By.name("password"));
        formPassword.sendKeys(Password2);
        WebElement formRemember = driver.findElement(By.name("remember"));
        formRemember.sendKeys(" ");
        WebElement formSubmit = driver.findElement(By.name("login_button"));
        formSubmit.click();

    the third element :

     driver.get("example_of_page");
        WebElement formEmail = driver.findElement(By.name("email"));
        formEmail.sendKeys(Email3);
        WebElement formPassword = driver.findElement(By.name("password"));
        formPassword.sendKeys(Password3);
        WebElement formRemember = driver.findElement(By.name("remember"));
        formRemember.sendKeys(" ");
        WebElement formSubmit = driver.findElement(By.name("login_button"));
        formSubmit.click();

    the fourth  element :

      driver.get( "example_of_page" );
            WebElement formEmail = driver.findElement(By.name("email"));
            formEmail.sendKeys(Email4);
            WebElement formPassword = driver.findElement(By.name("pass"));
        formPassword.sendKeys(Password4);
        WebElement formRemember = driver.findElement(By.name("persistent"));
        formRemember.sendKeys(" ");
        formPassword.submit();

}

How i can named, define this element in loop to run random element and start code from this place ? I use loop on this function, element and this element must not repeated on one loop.

4条回答
仙女界的扛把子
2楼-- · 2019-06-14 20:01

Here structure as you might begin, and make sure you can leave the block while, and not keep it in an infinite loop.

import java.util.Random;

public class StarAt {

  public static void main(String s[]) {
    while (true) {
        int element = new Random().nextInt(4)+1;
        switch (element){
            case 1: 
                    /*
                        Here put code first element
                    */          
                    break;
            case 2: 
                    /*
                        Here put code second element
                    */          
                    break;
            case 3: 
                    /*
                        Here put code third element
                    */          
                    break;
            case 4: 
                    /*
                        Here put code fourth element
                    */          
                    break;
        }
    }
  }
}
查看更多
不美不萌又怎样
3楼-- · 2019-06-14 20:04

You can get a random element of an array or List using Random

String[] words = "Hello All The World".split(" ");
String word = words[new Random().nextInt(words.length)];

Or for a List

List<String> words = 
String word = words.get(new Random().nextInt(words.size()));
查看更多
女痞
4楼-- · 2019-06-14 20:10

You can try Random or Math.random.

The following is a simple example:

Random random = new Random();
random.nextInt();

or

Math.random()
查看更多
乱世女痞
5楼-- · 2019-06-14 20:28

I think what you are asking for here is just basically picking a random element for 4 elements. One way of doing this would be adding those elements to an array.

Array[0] = element1
Array[1] = element2
Array[2] = element3
Array[3] = element4

Then you can get a random number in Java with

Random rand = new Random();
int randomNum = rand.nextInt((max - min) + 1) + min;

Here you can replace max and min with ur minimum number of elements and maximum number of elements. In this case its 0 to 3.

After getting this number, you can use it to index into the array of the elements.

 Array[randNum]

Hope this answer helps.

查看更多
登录 后发表回答