【问题标题】:How to write locator for a below tag element for WebDriver using Java? [duplicate]如何使用 Java 为 WebDriver 的以下标记元素编写定位器? [复制]
【发布时间】:2019-03-27 14:45:43
【问题描述】:

这是我需要单击的元素的 HTML 片段,我得到“org.openqa.selenium.InvalidSelectorException”

<svg viewBox="0 12.705 512 486.59" xmlns:xlink="http://www.w3.org/1999/xlink" 
xmlns="http://www.w3.org/2000/svg" height="20" width="20" style="margin-right: 
0px;">

<svg viewBox="0 12.705 512 486.59" 
xmlns:xlink="http://www.w3.org/1999/xlink" 
xmlns="http://www.w3.org/2000/svg" height="20" width="20" style="margin- 
right: 0px;">
<defs>
<polygon points="256.814,12.705 317.205,198.566 512.631,198.566 
354.529,313.435 414.918,499.295 256.814,384.427 98.713,499.295 
159.102,313.435 1,198.566 196.426,198.566 " style="fill: 
url(#jRate_grad4);stroke:black;fill-opacity:1;stroke-width:2px;">
</svg>

我使用了以下代码:

>WebElement ele = driver.findElement(By.xpath(".//*[@id='jRate']/svg[5]/"));
>JavascriptExecutor js = (JavascriptExecutor)driver;
>js.executeScript("argument[0].click()", ele);

我试图找到该元素并单击它,由于异常,我使用了 JavaScript 并且问题相同。我不知道我在这里错过了什么。

非常感谢 Java 相关的帮助。

【问题讨论】:

  • xpath中删除关闭/
  • 还是不行!
  • 从 xpath 中删除点并最后关闭 / - 使用它 - //*[@id='jRate']/svg[5]
  • @Dhru'soni 好的,改变了异常“org.openqa.selenium.NoSuchElementException”现在出现在屏幕上。
  • @SaiKrishnaPulivarthi 不要冒犯和批评“版主”,再看看你的问题并问自己“我的问题好吗?我是否分享了足够的信息以获得好的答案?”。我认为答案将是“不”。 This ticket 包含处理 svg 节点的解决方案。您的解决方案(来自评论)也与您的问题不匹配

标签: selenium selenium-webdriver css-selectors


【解决方案1】:

您的 xpath 无效。删除尾部斜杠

【讨论】:

  • 对不起,我在更改Xpath时忘记删除它,实际上即使删除它,错误也是一样的。 “org.openqa.selenium.NoSuchElementException”
  • 这是一个不同的错误信息。它表明它找不到具有该 xpath 的任何元素。如果没有看到 HTML,很难进一步提供帮助
  • w3.org/1999/xlink" xmlns="w3.org/2000/svg" height="20" width="20" style="margin-right :0像素; “>
  • Robbie,请检查上面的 HTML 代码,它不允许我添加到问题正文中,因此在这里。
  • 这是您希望返回的元素的 html,不是吗?您能否提供将在其中执行搜索的 HTML?
猜你喜欢
  • 1970-01-01
  • 2019-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-25
  • 1970-01-01
  • 1970-01-01
  • 2020-12-29
相关资源
最近更新 更多