【问题标题】:xpath does not get the web element which has multiple classesxpath 没有获取具有多个类的 web 元素
【发布时间】:2015-08-27 14:43:56
【问题描述】:

我是 Java 和 Selenium 的新手,所以如果我的问题听起来有点初级,我提前道歉。

我正在使用 Selenium 和 Java 编写测试。但是在寻找元素方面存在问题。我知道一些其他方法可以找到这个 WebElement,

但为什么会这样:

WebElement we1 =driverChrome.findElement(By.xpath
("//div[contains(@class,'elfinder-cwd-filename ui-draggable') and @title='project.CPG']"));

无法得到这个:

<div class="elfinder-cwd-filename ui-draggable" title="project.CPG">project.CPG</div>

并显示此错误:

Exception in thread "main" org.openqa.selenium.NoSuchElementException: no 
such element: Unable to locate element:{"method":"xpath","selector":"
//div[contains(@class,'elfinder-cwd-filename ui-draggable') and @title='project.CPG']"}

这行得通:

WebElement we1 = driverChrome.findElement(By
.xpath("//div[contains(@class,'elfinder-cwd-filename') and @title='project.CPG']"));

但这些不起作用:

WebElement we1 = driverChrome.findElement(By
    .xpath("//div[contains(@class,'ui-draggable') and @title='project.CPG']"));


WebElement we1 = driverChrome.findElement(By.
xpath("//div[@class='elfinder-cwd-filename ui-draggable' and @title='project.CPG']"));

听起来很奇怪,这是:

WebElement we = driverChrome.findElement(By
.xpath("//div[contains(@class,'combine-red')]"));

只是为此工作:

    <div class="leaflet-marker-icon combine-red-off leaflet-zoom-hide leaflet-
clickable" tabindex="0" style="margin-left: -17px; margin-top: -19px; left: 
149px; top: 302px; z-index: 10304; transform: rotate(450deg);"></div>

【问题讨论】:

  • 两者都应该工作,你试过像我提议的那样等待吗?可能有些类的加载时间比你预期的要晚一点
  • @drkthng driverChrome.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); driverChrome.findElement(By.xpath("//div[@class='elfinder-cwd-filename ui-draggable' and @title='project.CPG']")).click();没用
  • “没有工作”意味着它抛出了相同的错误消息,即找不到元素?你确定你的命令没有错别字吗?
  • @drkthng 它冻结了 30 秒并抛出 no such element: Unable to locate element

标签: java selenium testing selenium-webdriver selenium-chromedriver


【解决方案1】:

但为什么会这样:

WebElement we1 =driverChrome.findElement(By.xpath
("//div[contains(@class,'elfinder-cwd-filename ui-draggable') and @title='project.CPG']"));

无法得到这个:

 <div class="elfinder-cwd-filename ui-draggable" title="project.CPG">project.CPG</div>

这是因为 Selenium 的类选择器不支持复合类名称。它搜索确切的类名(复合类名的任何部分),而不是复合类名。

这段代码:

WebElement we1 = driverChrome.findElement(By.
xpath("//div[@class='elfinder-cwd-filename ui-draggable' and @title='project.CPG']"));

如果有一些额外的空格或者页面生成类名改变了它的位置,可能无法工作......比如:

'elfinder-cwd-filename ui-draggable''
'elfinder-cwd-filename   ui-draggable '

'ui-draggable elfinder-cwd-filename'

这里……

 WebElement we1 = driverChrome.findElement(By
     .xpath("//div[contains(@class,'ui-draggable') and @title='project.CPG']"));

你可能在一开始就错过了点。试试这个:

WebElement we1 = driverChrome.findElement(By
          .xpath(".//div[contains(@class,'ui-draggable') and @title='project.CPG']"));

【讨论】:

  • 我应该把那个点放在哪里以及为什么?
  • 它的意思是“所有
    元素在当前上下文中的一个或多个级别。”。我不确定这种情况,因为我没有看到 html。但这可能会解决您的任务。
  • List wes = driverChrome.findElements(By .xpath(".//div[contains(@class,'ui-draggable') and @title='project.CPG']" ));不会抛出任何异常但会在那里冻结
  • 请张贴html或给我链接(更好)
【解决方案2】:

当你写 contains 时,试着只提到一个类而没有任何空格。 IT 应该可以解决您的问题。方法如下 -

WebElement we1 =driverChrome.findElement(By.xpath
("//div[contains(@class,'elfinder-cwd-filename') and @title='project.CPG']"));

如果你想同时提到这两个类,那么就不要使用contains 标签。方法如下 -

WebElement we1 =driverChrome.findElement(By.xpath
("//div[@class='elfinder-cwd-filename ui-draggable' and @title='project.CPG']"));

希望这会有所帮助。

【讨论】:

  • 第一个有效,但我需要以相同的顺序检查两个类,第二个无效
  • @LoveLovelyJava 抱歉,语法有错误。更新了它。让我知道它是否有效。
【解决方案3】:

只需尝试“=”而不是包含:

WebElement we1 =driverChrome.findElement(By.xpath
("//div[@class='elfinder-cwd-filename ui-draggable' and @title='project.CPG']"));

如果它仍然不起作用,则可能是当您尝试查找该元素时该元素尚未在 DOM 中,那么您应该尝试在命令前放置一个隐式等待:

driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

【讨论】:

    猜你喜欢
    相关资源
    最近更新 更多
    热门标签