我想让它进入循环,即从下拉菜单中找到日期,并将其与存储在数据库中的日期进行匹配。. 然后选中它。否则测试用例失败。
for mean while。我是硒的初学者。所以我只是想给我的工作场所增加一些新东西。下拉名称为"出生年份",即有多年。
我很感激。由于
注意:可以从选项中选择每个值. 但是我的问题在这里添加一个循环条件来匹配数据库中的数据。
看看这个例子:
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"));