我有一个后退按钮在我的网页上,我需要点击从不同的地方。
在一页中,后退按钮xpath看起来像://property-list/ion-header/ion-navbar/button
在另一个页面中,它看起来是://select-client/ion-header/ion-navbar/button。
我正在使用POM --我可以使用一个对象来标识我的按钮可以拥有的所有xpath吗?
发布于 2017-02-28 06:18:22
我在Java上给出了答案:
您可以识别xpath的公共部分并在您的定位器中使用它。
根据上面提到的示例xpath's,您可以执行以下操作:
By backButtonLocator = By.xpath(".//ion-header/ion-navbar/button");
public void clickonElement(By backButtonLocator)
{
driver.findElement(backButtonLocator).click();
}@FindBy(how = How.XPATH, using =".//ion-header/ion-navbar/button")
public WebElement backButton;
public void clickonElement(WebElement ele)
{
ele.click();
}发布于 2017-02-28 11:18:15
如果the_coder的选择器不能工作,我建议让开发人员添加一个更好的方法来找到它。例如,添加一个ID。
或者,您也可以这样做:
back() {
Boolean found = false;
try {
driver.findElement("//property-list/ion-header/ion-navbar/button").click();
found = true;
} catch {
// Ignore notfoundexception
}
try {
driver.findElement("//select-client/ion-header/ion-navbar/button").click();
found = true;
} catch {
// Ignore notfoundexception
}
if (found === false) throw new Exception('backButtonNotFound');
}只需尝试单击两种不同的路径,如果成功,就会抛出异常。
发布于 2017-02-28 17:17:29
您可以使用类似尼尔斯所建议的逻辑。
您还可以在页面对象中执行如下操作:
// find my button - first, get all button elements on the page
// then use System.Linq to find the button in the collection
// with something in its innerHtml that only the back button will have
// Everything has been declared elsewhere in the unit.
try
{
button = driver.findElements(By.Type, "button").Where(c -> c.InnerHtml.Contains("Back").First();
}
catch (ElementNotFoundException e)
{
// do something to handle not finding the button
}https://sqa.stackexchange.com/questions/25776
复制相似问题