首页
学习
活动
专区
圈层
工具
发布

单击Xpath
EN

Stack Exchange QA用户
提问于 2017-02-28 04:17:08
回答 3查看 919关注 0票数 2

我有一个后退按钮在我的网页上,我需要点击从不同的地方。

在一页中,后退按钮xpath看起来像://property-list/ion-header/ion-navbar/button

在另一个页面中,它看起来是://select-client/ion-header/ion-navbar/button

我正在使用POM --我可以使用一个对象来标识我的按钮可以拥有的所有xpath吗?

EN

回答 3

Stack Exchange QA用户

发布于 2017-02-28 06:18:22

我在Java上给出了答案:

您可以识别xpath的公共部分并在您的定位器中使用它。

根据上面提到的示例xpath's,您可以执行以下操作:

只使用POM:

代码语言:javascript
复制
By backButtonLocator = By.xpath(".//ion-header/ion-navbar/button");

public void clickonElement(By backButtonLocator)
{
  driver.findElement(backButtonLocator).click();
}

使用PageFactory:

代码语言:javascript
复制
@FindBy(how = How.XPATH, using =".//ion-header/ion-navbar/button")
public WebElement backButton;

public void clickonElement(WebElement ele)
{
  ele.click();
}
票数 1
EN

Stack Exchange QA用户

发布于 2017-02-28 11:18:15

如果the_coder的选择器不能工作,我建议让开发人员添加一个更好的方法来找到它。例如,添加一个ID。

或者,您也可以这样做:

代码语言:javascript
复制
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');
}

只需尝试单击两种不同的路径,如果成功,就会抛出异常。

票数 0
EN

Stack Exchange QA用户

发布于 2017-02-28 17:17:29

您可以使用类似尼尔斯所建议的逻辑。

您还可以在页面对象中执行如下操作:

代码语言:javascript
复制
// 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
}
票数 0
EN
页面原文内容由Stack Exchange QA提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://sqa.stackexchange.com/questions/25776

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档