Selenium Webdriver with Java:如何从下拉选择选项菜单中选择随机出生月份和随机国家/地区

Selenium Webdriver with Java:如何从下拉选择选项菜单中选择随机出生月份和随机国家/地区,java,webdriver,Java,Webdriver,我用虚拟用户填充我的网站,除了出生月份和国家/城市之外,我已经随机化了所有东西 你能帮我找到一个简单的方法吗? 这是我现在使用的代码,但它已硬编码到其中:( 以下是国家/地区下拉列表的html: <div class="custom-select"> <select id="user_country_id" class="validate" name="user[country_id]" data-cities-path="/country/id-replace/cit

我用虚拟用户填充我的网站,除了出生月份和国家/城市之外,我已经随机化了所有东西

你能帮我找到一个简单的方法吗? 这是我现在使用的代码,但它已硬编码到其中:(

以下是国家/地区下拉列表的html:

<div class="custom-select">
    <select id="user_country_id" class="validate" name="user[country_id]" data-cities-path="/country/id-replace/cities">
        <option value="51baca9bf325db50be000012">Japan</option>
        <option value="51baca9bf325db50be000015">China</option>
        <option value="51baca9bf325db50be00001c">Australia</option>
        <option value="51baca9bf325db50be000023">Thailand</option>
    </select>
</div>

日本
中国
澳大利亚
泰国

您可以将标签放置在一个数组中,然后使用类从中选择一个随机元素。然后,当您生成一个国家的随机名称时,您可以将其传递给
select
对象

String[] countriesArray = {"Japan", "Germany", "Australia", "Thailand"};
String randomLabel = countriesArray[new Random().nextInt(countriesArray.length)];

Select user_country = new Select (driver.findElement(By.id("user_country_id")));
user_country.selectByVisibleText(randomLabel);

您可以将标签放置在数组中,然后使用类从中选择一个随机元素。然后,当您生成一个国家的随机名称时,可以将其传递给
select
对象

String[] countriesArray = {"Japan", "Germany", "Australia", "Thailand"};
String randomLabel = countriesArray[new Random().nextInt(countriesArray.length)];

Select user_country = new Select (driver.findElement(By.id("user_country_id")));
user_country.selectByVisibleText(randomLabel);

你为什么不按随机索引选择呢

user_country.selectByIndex(new Random().nextInt(user_country.getOptions().size()));

你为什么不按随机索引选择呢

user_country.selectByIndex(new Random().nextInt(user_country.getOptions().size()));

我设置它的方法是为随机变量使用单独的方法,例如:

public int RandomSelectInt(List<IWebElement> elements)
{
    int options = elements.Count;
    Random random = new Random();
    return random.Next(1, options);
}
public int random selectint(列表元素)
{
int options=elements.Count;
随机=新随机();
返回random.Next(1,选项);
}
然后,将项目添加到列表时,可以在代码中调用此方法,如:

public void selectCountry()
{ 
    IWebElement countryOptions = driver.FindElement(By.id("user_country_id");
    List<IWebElement> countryList = countryOptions.FindElements(By.TagName("Option")).ToList(); 
    new SelectElement(countryOptions).SelectByIndex(MethodPath.RandomSelectInt(countryList));
}
public void selectCountry()
{ 
IWebElement countryOptions=driver.FindElement(By.id(“用户\国家\ id”);
List countryList=countryOptions.FindElements(按.TagName(“Option”)).ToList();
新的SelectElement(countryOptions).SelectByIndex(MethodPath.RandomSelectInt(countryList));
}

我希望这有助于

我设置它的方法是为随机变量设置一个单独的方法,例如:

public int RandomSelectInt(List<IWebElement> elements)
{
    int options = elements.Count;
    Random random = new Random();
    return random.Next(1, options);
}
public int random selectint(列表元素)
{
int options=elements.Count;
随机=新随机();
返回random.Next(1,选项);
}
然后,将项目添加到列表时,可以在代码中调用此方法,如:

public void selectCountry()
{ 
    IWebElement countryOptions = driver.FindElement(By.id("user_country_id");
    List<IWebElement> countryList = countryOptions.FindElements(By.TagName("Option")).ToList(); 
    new SelectElement(countryOptions).SelectByIndex(MethodPath.RandomSelectInt(countryList));
}
public void selectCountry()
{ 
IWebElement countryOptions=driver.FindElement(By.id(“用户\国家\ id”);
List countryList=countryOptions.FindElements(按.TagName(“Option”)).ToList();
新的SelectElement(countryOptions).SelectByIndex(MethodPath.RandomSelectInt(countryList));
}
我希望这有帮助

试试这个

试试这个


ID是固定的还是动态生成的?ID是固定的还是动态生成的?但是有200多个国家/地区,我如何动态获取这些国家/地区,以便将它们放入一个数组中?我刚刚缩短了示例,因此我们将其保留到重点。我不想手动编写。但是有200多个国家/地区,我如何动态地获取它们获取这些,这样我就可以将它们放入数组中?我刚刚缩短了示例,所以我们将其保持在要点上..我不想手动编写。惊人的是,这正是我想要的-一个简单的方法惊人的是,这正是我想要的-一个简单的方法