AngularJs页面问题,选择一个元素并单击它

时间:2017-03-22 08:40:04

标签: java angularjs selenium

我在选择和点击元素时出现问题,因此这里发生的下拉是我现在尝试过的: -

String csspath = "html body.ng-scope f:view form#wdesk.ng-pristine.ng-valid div.container div.ng-scope md-content.md-padding._md md-tabs.ng-isolate-scope.md-dynamic-height md-tabs-content-wrapper._md md-tab-content#tab-content-7._md.ng-scope.md-active.md-no-scroll div.ng-scope.ng-isolate-scope ng-include.ng-scope div.ng-scope accordion div.accordion div.accordion-group.ng-isolate-scope div.accordion-heading a.accordion-toggle.ng-binding span.ng-scope b.ng-binding";
String uxpath = "//html//body//f:view//form//div//div[2]//md-content//md-tabs//md-tabs-content-wrapper//md-tab-content[1]//div//ng-include//div//accordion//div//div[1]//div[1]//a";
String xpath2 = "/html/body/pre/span[202]/a";
xpath = "/html/body/f:view/form/div/div[2]/md-content/md-tabs/md-tabs-content-wrapper/md-tab-content[1]/div/ng-include/div/accordion/div/div[1]/div[1]/a/span/b";
try {
  element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(csspath)));

  locator = By.cssSelector(csspath);
  driver.findElement(locator).click();
} catch (Exception e) {
  System.out.println("Not foune csspath");
}



try {
  element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(xpath)));

  locator = By.xpath(xpath);
  driver.findElement(locator).click();
} catch (Exception e) {
  System.out.println("Not foune xpath");
}

try {
  element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(uxpath)));

  locator = By.xpath(uxpath);
  driver.findElement(locator).click();
} catch (Exception e) {
  System.out.println("Not foune uxpath");
}

try {
  element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(xpath2)));

  locator = By.xpath(xpath2);
  driver.findElement(locator).click();
} catch (Exception e) {
  System.out.println("Not foune xpath2");
}

但是到目前为止我还没有任何工作要选择责任代码并给出值

如果您能给我任何见解,我们将非常感激 提前致谢 这是我的问题的截图 enter image description here

1 个答案:

答案 0 :(得分:0)

第一个问题(正如评论中已经指出的那样)是您正在使用的绝对选择器。例如,尝试重构xpath选择器并生成relative

下一期与

有关
  

AngularJs页面

本身。让我们看一下基于WebDriverJS的Angular测试框架Protractor,它提供了额外的类似WebDriver的功能来测试基于Angular的网站。简单一点 - 您的代码需要额外的功能,以便知道Angular元素何时可用于交互。

这是how to port Java(和Python)最有用的一些Protractor函数:

enter image description here