我正在尝试使用此代码在网页上找到按钮(预览/编辑),在该按钮中,它将根据参数text(buttonName)单击按钮。

该页面的行为{当页面加载时,它显示给您“编辑”按钮。如果单击“编辑”,则会显示“预览”按钮,反之亦然}

WebBrowserElement parentElement = waitForElement(By.xpath("//div[@class='serviceHeaderInner']"));
String s = parentElement.toString();
if (s.contains(buttonName) && (parentElement.waitForElement(By.xpath(".//a[.='" + buttonName + "']"))).isDisplayed()) {
    WebBrowserElement buttonElement = parentElement.waitForElement(By.xpath(".//a[.='" + buttonName + "']"));
    buttonElement.click();
} else {
    debugPrintln("        -> Invalid  buttonName '"+ buttonName +"'");
}


在我试图通过的测试案例中,

'预习'

'预习'

'编辑'

'编辑'

当页面加载时,将有“编辑”按钮并在测试用例中两次传递“预览”参数。它直接进入else等等。此外,传递“ Edit”参数也进入else部分。我猜“ if”条件有问题。但要使其更具动态性,“它应检查快照中包含“已禁用”文本的类,以便可以在第一点避免此问题。

快照:
12

最佳答案

您可以使用以下方法根据名称单击按钮。在此方法上使用try{} catch(){}处理按钮不存在时的情况。

// button names can be one of [Edit, Preview]
// Provided you have only one button each on the name of Edit and Preview on your page
public void clickButtonBasedOnName(String buttonName) {
    waitForElement(By.linkText(buttonName)).click();
}

10-07 19:21
查看更多