Selenium IDE 定位器在 Selenium 3.3.1 Java 中不起作用

Selenium IDE locator doesn't work in Selenium 3.3.1 Java

我有一个网页,其中包含 2 个具有相同类名但 div 类 的 link。第一个是不可见的(在下拉菜单中),我想要的另一个是可见的。 所以,我试图找到可见元素。

他的HTML:

 <div class="mainActionPanel">
   <a css="create"></a>
 </div>

link 有一个动态 ID。当我使用 ID 进行 XPath 搜索时,我正确地找到了该元素,但它已被弃用,因为该按钮在每个页面上的 ID 不同。

我尝试用 Selenium IDE 定位元素,以下定位器有效:css=div.mainActionPanel > a.create

问题出在我上面显示的定位器上。 当我试图找到元素时,我总是有这个例外:

NoSuchElementException : Element By.cssSelector: css=div.mainActionPanel > a.create (first) (LazyElement) is not present.

他没有找到。我尝试了几种语法,例如 FluentLenium 文档 ($("form > input[data-custom=selenium]") 中的示例,但没有用。

此外,el(".create").click() 抛出 ElementNotVisibleException,因为他选择了下拉菜单 link。

如何找到正确的元素?

也许这对你有帮助..

 // this will provide you list of web elements based on class name
 List<WebElement> webElements = driver.findElements(By.className("mainActionPanel"));

然后您必须通过遍历列表来找出所需的元素,如下所示。

  WebElement tempElement = null;
  for (WebElement element : webElements) {

            if (element.getAttribute("css").equals("create")) {
                tempElement = element;
            }
        }

完整代码如下,

  List<WebElement> webElements = driver.findElements(By.className("mainActionPanel"));

  WebElement tempElement = null;
  for (WebElement element : webElements) {

            if (element.getAttribute("css").equals("create")) {
                tempElement = element;
            }
  }


  //then you can perform that you want
  tempElement.click();

已从 css 搜索更改为 xpath 使用:

xpath = (//a[contains(@href, '#')])[5]

因为它是第 4 个 "a" 元素,其 href 属性包含文本“#”。没有更多的麻烦。

看看这个: http://www.guru99.com/locators-in-selenium-ide.html

尝试使用以下方法:

 WebElement elem = driver.findElement(By.cssSelector("div.mainActionPanel > a"));
 WebDriverWait wait= new WebDriverWait(driver, 20);

 wait.until(ExpectedConditions.visibilityOf(elem));

 elem.click();