【问题标题】:Automate clicking on JavaScript link with Selenium使用 Selenium 自动点击 JavaScript 链接
【发布时间】:2013-03-22 13:42:22
【问题描述】:

我正在尝试自动单击在网页上异步运行的数据库的第 2 页。使用 Xpath 时:

//id('standardView')/x:div[3]/x:div[3]/x:a[1]

我收到以下错误:

[error] Invalid xpath [2]: //id('rightMenu')/x:div[3]/x:div[3]/x:a[1]

所以我尝试使用 DOM 来查找它的路径,下面的代码显示了使用 Firebug 运行时被推送的实际链接...

   main = document.getElementById("rightMenu"); mainCont = main.contentDocument;
   paging = mainCont.getElementsByTagName("a"); pageTwo = paging[0];

Firebug 将显示:

<a href="javascript:chgPage(2);">

但是 Selenium 抛出了这个错误:

[error] Unexpected Exception: fileName -> chrome://selenium-ide/content/selenium-
core/scripts/selenium-api.js, lineNumber -> 2535, columnNumber -> 45

而且只是简单地记录动作并没有发现按钮被按下。我似乎找不到解决方法。有什么见解吗朋友们

【问题讨论】:

  • 你能发布一个 html 的 sn-p 吗?

标签: dom xpath selenium automation selenium-ide


【解决方案1】:

有点奇怪的 xpath。尝试使用//*[@id='standardView']/div[3]/div[3]/a[1]

但尽量使用不带索引的选择器,并且尽可能短

//a[@href='javascript:chgPage(2);']

或

//*[@id='standardView']//a[@href='javascript:chgPage(2);']

这些也可以工作(如果它们是唯一的,请使用 firepath 插件检查这一点)

【讨论】:

  • 谢谢,亚历山大!我使用了 //a[@href='javascript:chgPage(2);'] 并且效果很好。
猜你喜欢
  • 1970-01-01
  • 2018-10-11
  • 2013-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多