PhantomJS单击一个元素,但不单击另一个元素



我有一个问题。我一直在尝试单击一个元素。这是源代码的一个片段:

<a onclick="edit_subj()">Edit topics</a>
<a onclick="create_new('site')">
  <img src="img/new.gif" width="10" height="10" border="0" 
   alt="New" title="New site"> Create new site
</a>

当我使用 PhantomJS 并单击更高的元素时:

phantomJSDriver.executeScript("edit_subj();"); 

它有效,但是当我尝试单击另一个元素时:

phantomJSDriver.executeScript("create_new('site')"); 

我收到一个错误:

[ERROR - 2018-01-22T17:12:10.828Z] Session [f6c23730-ff96-11e7-90c5-
5d358d18a7d9] - page.onError - msg: ReferenceError: Can't find 
variable: create_new
:262 in error
[ERROR - 2018-01-22T17:12:10.828Z] Session [f6c23730-ff96-11e7-90c5-
5d358d18a7d9] - page.onError - stack:
onclick (http://WEBSITE_ADDRESS/:156)
dispatchEvent (:0)
U (phantomjs://webpage.evaluate():119)
$ (phantomjs://webpage.evaluate():108)
$ (phantomjs://webpage.evaluate():101)
gh (phantomjs://webpage.evaluate():141)
sh (phantomjs://webpage.evaluate():152)
(anonymous function) (phantomjs://webpage.evaluate():152)
(anonymous function) (phantomjs://webpage.evaluate():152)
(anonymous function) (phantomjs://webpage.evaluate():153)

编辑:有些人要求我使用更多的例子:

WebElement createNewSite = phantomJSDriver
               .findElement(By.id("edit"))
               .findElements(By.tagName("a")).get(1);
createNewSite.click();
new Actions(phantomJSDriver)
.moveToElement(createNewSite)
.moveByOffset(0, 0)
.click()
.perform();
 phantomJSDriver.executeScript("create_new('site')"); 
 ((JavascriptExecutor)phantomJSDriver)
  .executeScript("arguments[0].click();", createNewSite); 
  phantomJSDriver
   .findElement(By.xpath("//a/img[@src='img/new.gif']"))
   .click();
  new Actions(phantomJSDriver)
   .moveToElement(createNewSite).click().perform();

这些都是单独的例子。等待无济于事 - 问题在于我可以单击其他标签,但无法单击我需要的标签。所有这些示例都会引发相同的错误:

Can't find  variable: create_new

真的没有想法了,我尝试通过引用标签内的 img 标签来按标签名称单击它。它总是相同的错误。更糟糕的是,它曾经工作过,但几个月前突然停止工作。请帮助我,提前谢谢你!:)

从你的问题中不清楚你为什么要使用Javascript click().要click() text的链接Create new site您可以使用以下代码行:

phantomJSDriver.findElement(By.xpath("//a/img[@src='img/new.gif']")).click();

最新更新