如何 select 从循环中的列表选项 select 每个选项并从数据库中找到值 - 使用 selenium webdriver - JAVA

How to select from list options within loop through them select each option and find the value from database - using selenium webdriver - JAVA

我希望它在 loop 中,从下拉列表中找到日期并将其与存储在数据库中的日期相匹配.然后 select it.otherwise 测试用例失败。

同时。我是硒的初学者。所以我只想在我的工作场所添加一些新东西。 下拉名称是“Year of Birth”,有多个年份。

我将不胜感激。谢谢

注意:能够从每个值的选项select。但是我的问题在这里添加一个循环条件来匹配数据库中的数据。

看看这个例子:

package selenium;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;

public class Malikah extends WebDriverSetup {

    public static void main(String[] args) {
        
        WebDriver driver = startChromeDriver(); // standard chromedriver init
        driver.get("https://www.globalsqa.com/demo-site/select-dropdown-menu/");
        WebElement dropDown = driver.findElement(By.tagName("select"));
        Select select = new Select(dropDown);
        List<WebElement> options = select.getOptions();
        for (WebElement option: options) {
            select.selectByValue(option.getAttribute("value"));
            System.out.println("Selected option's text: " + select.getFirstSelectedOption().getText());
        }
        driver.quit();
    }
    
}

输出:

Starting ChromeDriver 91.0.4472.101 (af52a90bf87030dd1523486a1cd3ae25c5d76c9b-refs/branch-heads/4472@{#1462}) on port 39875
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
[1627992309.880][WARNING]: This version of ChromeDriver has not been tested with Chrome version 92.
Srp 03, 2021 2:05:09 ODP. org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
Selected option's text: Afghanistan
Selected option's text: ?land Islands
Selected option's text: Albania
Selected option's text: Algeria
Selected option's text: American Samoa
Selected option's text: Andorra
Selected option's text: Angola
...

使用

解决
driver.findElement(YearOfBisrth).click();
        Select selectBirth = new Select(driver.findElement(YearOfBisrth));
        driver.findElement(YearOfBisrth).click();
        selectBirth.selectByValue(data.get("YearBirth"));