我正在测试一个股票网站
我在每只股票的页面上都有一个特定的“时钟”,显示该股票当前是否开放/关闭进行交易
closed : class="inlineblock redClockBigIcon middle isOpenExchBig-1"
opened : class="inlineblock greenClockBigIcon middle isOpenExchBig-1014"
唯一的属性是“类”。我想使用“if”语句以便区分它们,我尝试在“关闭”状态下运行它(请参阅下面的“检查”代码,距底部 12 行)。
它在循环的第三次抛出异常:
org.openqa.selenium.NoSuchElementException: 没有这样的元素
为什么?请问我该如何解决?
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "C:\\automation\\drivers\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.investing.com");
driver.navigate().refresh();
driver.findElement(By.cssSelector("[href = '/markets/']")).click();;
// list |
int size = 1;
for (int i = 0 ; i < size ; ++i) {
List <WebElement> list2 = driver.findElements(By.cssSelector("[nowrap='nowrap']>a"));
//Enter the stock page
size = list2.size();
Thread.sleep(3000);
list2.get(i).click();
**//Check**
WebElement Status = null;
if (Status == driver.findElement(By.cssSelector("[class='inlineblock redClockBigIcon middle isOpenExchBig-1']")))
{
System.out.println("Closed");
}
// Print instrument name
WebElement instrumentName = driver.findElement(By.cssSelector("[class='float_lang_base_1 relativeAttr']"));
System.out.println(instrumentName.getText());
Thread.sleep(5000);
driver.navigate().back();
}
}
}
尝试使用
WebElement Status = null;
if (Status == driver.findElement(By.className("redClockBigIcon")))
{
System.out.println("Closed");
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句