我的测试中有一个步骤进入多个 html 页面并在屏幕上查找元素。该元素可以有 2 个不同的 CSS 类名称,但在网站中看起来相同(从视觉上来说),我必须使用带有逻辑“或”的 if 语句来识别它们
if (Status == driver.findElement(By.cssSelector(".inlineblock.redClockBigIcon.middle.isOpenExchBig-2")) || Status == driver.findElement(By.cssSelector(".inlineblock.redClockBigIcon.middle.isOpenExchBig-1")));
System.out.println("Stock is closed");)
我预计如果出现 2 个元素之一,Eclipse 会出现
认识它。好吧 - 2 中的第二个元素出现了 - 出于某种原因,我遇到了异常错误。 if 语句只关注 if 中的第一个条件,而忽略第二个。
org.openqa.selenium.NoSuchElementException: no such element:
locate element: {"method":"css selector","selector":".inlineblock.redClockBigIcon.middle.isOpenExchBig-2"}Unable to
我怎样才能制作||在这个“if”语句中工作? 谢谢
最佳答案
在您的上述逻辑中,您有 Status
,这是一个已经存在的 WebElement
,您正在将其与另一个 Webelement
进行比较抬头。我认为这不是您的意图,因此我将在解决方案中做出一些假设。
首先:找到您想要的选择器可能存在的所有元素(注意我使用的是 findElements
而不是 findElement
)
List<WebElement> clockIconThingies = driver.findElements(By.cssSelector(".inlineblock.redClockBigIcon.middle.isOpenExchBig-2, .inlineblock.redClockBigIcon.middle.isOpenExchBig-1"));
其次:检查是否发现了什么
if(clockIconThingies.size() > 0)
{
System.out.println("Stock is closed");
}
对于您的 css 选择器,从图像上看您可能根本不需要执行 或 ,只需像这样查找 redClockBigIcon
类:
List<WebElement> clockIconThingies = driver.findElements(By.cssSelector(".redClockBigIcon"));
关于css - Selenium WD |尝试将 'If' 语句与逻辑 'or' 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47642781/