如何从硒中的嵌套XPath获得价值

发布于 2025-02-07 18:43:58 字数 470 浏览 2 评论 0原文

我正在尝试使用Selenium和Java测试一个我想要实现的应用程序。

单击一个选择框(有效的框),然后从多个selectoption中选择一个等于我的输入的框。因此,如果我的功能文件说狗,我希望从“选项狗”中选择狗。

打开选择后选择选项的代码看起来像这样:

WebElement xyz = driver.findElement(By.xpath(String.format("//*[@id='xyz']/div[2]/ul/li[text()='%s']",selectElement)));

如果我删除 /li,则在浏览器中的控制台中选择每个选项,所以我想使用/li [text()='%s'] < /代码>我可以动态设置选项名称。但是我得到了鼻nocelementExcetion,我的错误可能是什么?如果我正确1而不是text()='%s'我可以选择选项

I am trying to test an application with Selenium and Java, what I want to achieve, is the following.

Click on a select box (which works) and then select from a multiple selectoption the one equaling my input. So if my Feature file says dog I want from the options dog to be selected.

My code to select the option after opening the selectbox looks like this:

WebElement xyz = driver.findElement(By.xpath(String.format("//*[@id='xyz']/div[2]/ul/li[text()='%s']",selectElement)));

If I delete /li every option is selected in my console in the browser, so I thought with /li[text()='%s'] I could dynamically set the option name. But I am getting a nosuchelementexcetion, what could be my mistake? If i right 1 instead of text() = '%s' I am being able to select the option

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深海夜未眠 2025-02-14 18:43:58

对于选择框,您需要创建一个针对选择框的选择对象,然后按值选择要选择要选择的项目。例如:

Select drpDown = new Select(driver.findElement(By.xpath(//pathToElement]")));

drpDown.selectByValue(selectElement);

希望这会有所帮助:)

For the select box, you need to create a Select object which targets the select box and then select by value to get the item you want to select. For example:

Select drpDown = new Select(driver.findElement(By.xpath(//pathToElement]")));

drpDown.selectByValue(selectElement);

Hope this helps :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文