gpt4 book ai didi

java - Selenium 警报仅在 Debug模式下显示

转载 作者:行者123 更新时间:2023-12-01 10:01:57 25 4
gpt4 key购买 nike

我正在使用 Webdriver/Java 测试一个包含许多字段的网站页面。其中一个字段是下拉框(或选择框),它包含失去焦点时的验证(例如跳出)。因此,如果我在下拉列表中选择特定选项并选择下一个字段,则会弹出一个警告框,提示“您不能选择该选项!”。

现在我正在编写代码来检查警报框是否存在并接受它,但这仅在 Debug模式下适用于我。运行测试时(即不在调试中),我收到“等待警报出现 10 秒后超时构建信息:版本:'2.53.0'”。

我知道这可能是一个计时问题,因为它在 Debug模式下工作,但我无法理解为什么,因为我正在使用 ExpectedConditions.alertIsPresent()。失败的代码在这里:

WebElement currentElement = driver.findElement(By.id("selectbox")); 
Select currentSelect = new Select(currentElement);
currentSelect.selectByVisibleText(updatedValue);
currentElement.sendKeys(Keys.TAB);

System.out.println("milestoneA");
if ((exceptionExpected()) {
System.out.println("milestoneB");
wait.until(ExpectedConditions.alertIsPresent());
System.out.println("milestoneC");
checkAlertBox(getExpectedResultFromExcel());
}

在 Debug模式下,代码继续正常运行,一切都很好。在我的日志中的运行模式下,我达到了milestoneB,然后抛出了上述错误。

此外,如果我在 Tabbing 之前添加 Thread.sleep(1000),一切正常。

请问有什么想法吗?

最佳答案

我会尝试发送TAB键,直到元素失去焦点:

WebElement currentElement = driver.findElement(By.id("selectbox"));
currentElement.click();
currentElement.sendKeys("abcd");

// wait for the popup to be visible
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#abc")));

// send the TAB key while the current element has focus
new WebDriverWait(driver, 20).until((WebDriver wd) -> {
currentElement.sendKeys(Keys.TAB);
return !wd.switchTo().activeElement().equals(currentElement);
});

关于java - Selenium 警报仅在 Debug模式下显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36741669/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com