gpt4 book ai didi

java - 如何检查 Selenium 选择器是否成功?

转载 作者:搜寻专家 更新时间:2023-11-01 03:35:26 25 4
gpt4 key购买 nike

我目前正在使用 Appium 为网站开发自动化 UI 测试。我在 testobject 上使用许多设备运行测试,我尝试解决一些问题。

我的示例代码是这样的:

WebElement lexiconCollapsible = mDriver.findElement(By.xpath("//*[@id='1014']/a"));
assertNotNull(lexiconCollapsible);
ScrollHelper.scrollToElement(mDriver,lexiconCollapsible);
Thread.sleep(1000);
lexiconCollapsible.click();

这适用于许多设备,但不适用于所有设备。在一些我得到以下错误代码:

org.openqa.selenium.InvalidSelectorException: Argument was an invalid selector (e.g. XPath/CSS). (WARNING: The server did not provide any stacktrace information)

在我要点击元素的位置抛出异常,所以对象不为null。

所以我的问题是:有没有人找到解决方案来检查设备是否能够找到对象?是否有类似 isObjectFound 的方法?

我也尝试过使用 css 选择器、id 等,但结果是一样的。

最佳答案

来自 Selenium 文档,

exception selenium.common.exceptions.InvalidSelectorException(msg=None, screen=None, stacktrace=None)[source]
Thrown when the selector which is used to find an element does not return a WebElement. Currently this only happens when the selector is an xpath expression and it is either syntactically invalid (i.e. it is not a xpath expression) or the expression does not select WebElements (e.g. “count(//input)”).

所以看起来您的选择器不正确,因为它只发生在 XPATH 中,您应该尝试 css 选择器。

尝试,

WebElement lexiconCollapsible = mDriver.findElement(By.cssSelector("#1014 a")).click();

关于您的 isObjectFound 方法,看起来当您执行 findElement 时找到了 WebElement,您仅在 click() 上遇到异常>。我建议您切换到 CSS 选择器以避免此异常。

关于java - 如何检查 Selenium 选择器是否成功?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33299503/

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