gpt4 book ai didi

macos - Selenium WebDriver - 无法在 Mac OS X 上的 Chrome 中关闭选择下拉菜单

转载 作者:行者123 更新时间:2023-12-03 15:28:21 30 4
gpt4 key购买 nike

我已经使用 Selenium WebDriver 几个月了,我正在处理的 Web 应用程序中的下拉菜单出现问题。

正在发生的事情是测试正在打开页面,通过查找页面上的几个元素然后确保它们被显示来验证它们。
这样做之后,在不同的字段中输入了一些文本,然后单击选项选择框以打开下拉菜单。
在此之后,测试遍历下拉菜单中的所有选项,直到找到所需的选项,然后单击该选项。

此时该选项已被选中,但下拉菜单并未关闭。

我曾尝试再次单击选项选择,但这没有效果,在其余的测试期间,导航到其他页面并且菜单没有关闭。

然后页面被保存,然后导航离开。
但是下拉菜单会一直保留到浏览器关闭。

这是应用程序中的代码:

<select id="options" name="options" class="options">
<option value="option1 (auto)">option1 (auto)</option>
<option value="option2">option2</option>
<option value="option3">option3</option>
</select>

最佳答案

我会尝试的第一个解决方案是以不同的方式单击菜单选项。 Selenium API 为我们提供了这种可能性。
1) 定位例如元素的 css 选择器。

String cssOption1 = "select[id='options']>option[value='option1 (auto)']";
String cssOption2 = "select[id='options']>option[value='option2']";
String cssOption3 = "select[id='options']>option[value='option3']";

也不要忘记验证您是否正确找到了元素,例如 .in firepath、ffox 中的 firebug 插件:
proper location on web element in firepath

方法一
driver.findElement(By.cssSelector(cssOption2)).click();

使用操作构建器 API 的方法 2
WebElement mnuOptionElement;
mnuOptionElement = driver.findElement(By.cssSelector(cssOption2));
Actions builder = new Actions(driver);
// Move cursor to the Main Menu Element
builder.moveToElement(mnuOptionElement).click();

您可以获得有关操作构建器的更多信息 here

方法 3 使用 jsExecutor 单击 web 元素。在所有情况下都对我有用。
JavascriptExecutor js = (JavascriptExecutor) driver;
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("var x = $(\'"+cssOption2+"\');");
stringBuilder.append("x.click();");
js.executeScript(stringBuilder.toString());

希望这对你有用

关于macos - Selenium WebDriver - 无法在 Mac OS X 上的 Chrome 中关闭选择下拉菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12784108/

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