findElement方法显示通过Java使用Selenium的NoSuchElementException

问题描述 投票:-1回答:1

我需要单击一个可能以50%的机会出现的按钮,决定对findElementBy()使用try / catch。但是,try/catch不起作用,我遇到了异常。也许有一种更有效的方法来处理该按钮?

driver.manage().timeouts().implicitlyWait(5000, TimeUnit.MILLISECONDS);
WebDriverWait wait = new WebDriverWait(driver,5);
try{
   WebElement element = driver.findElement(By.xpath("buttonXpath"));
   element.click();
}
catch (NoSuchElementException e){ }
java selenium try-catch webdriverwait nosuchelementexception
1个回答
-1
投票

使用方法检查此元素是否在屏幕上:

if (!driver.findElementsByXPath("buttonXpath`enter code here`").isEmpty()) {
   driver.findElementByXPath("buttonXpath`enter code here`").click();
}
© www.soinside.com 2019 - 2024. All rights reserved.