java – 如何检查selenium选择器是否成功?

我目前正在使用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 Docs,

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)”).

所以看起来你的Selector是不正确的,因为它只发生在XPATH,你应该尝试css选择器.

尝试,

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

关于你的isObjectFound方法,当你执行findElement时,它看起来像是找到了WebElement,你只是在click()上得到一个例外.我建议您切换到CSS选择器以避免此异常.

点赞