找不到定位符时如何跳过特定的WebElement进程?

时间:2018-09-05 14:28:34

标签: selenium

`prinBalAgencyComm.sendKeys(testData.get(“ agencyCommissionPB”));

prinBalClientRem.sendKeys(Keys.TAB);

prinBalFrom2.sendKeys(testData.get(“ fromFB2”)); //未找到定位符

prinBalAgencyCommLast.sendKeys(testData.get(“ agencyCommissionLastPB”)); //如何执行此行而不会失败`

3 个答案:

答案 0 :(得分:1)

您可以通过使用try catch最终解决此问题。

try{
    //code that can result in an exception
    prinBalAgencyComm.sendKeys(testData.get("agencyCommissionPB"));
    prinBalClientRem.sendKeys(Keys.TAB);
    prinBalFrom2.sendKeys(testData.get("fromFB2"));
}catch(Exception e)
{   
    //actions you want to take in case your locator isnt found or another exception occurs
    System.out.println("Exception occured" + e.getMessage());
}finally
{   //the line to be executed without fail
    prinBalAgencyCommLast.sendKeys(testData.get("agencyCommissionLastPB"));
}

答案 1 :(得分:0)

您将必须实现try / catch块

try {
element action
} catch (Exception e) {
//whatever you want to happen when it fails
}`

但是我会问自己为什么测试与每次运行不一致?为什么每次都不应该跳过此步骤?

答案 2 :(得分:-1)

您可以使用的一种模式是:

try {}
catch {}

将有问题的代码放在try块中,并将任何错误代码放在catch块中。

如果try中引发异常,则将调用catch块,而不是继续进行try中的下一行