how can I click a link using CasperJS without knowing the css selector

拈花ヽ惹草 提交于 2019-12-22 06:58:13

问题


<a href="pss.exe?TRANSACTION=CGI_JUMP&amp;SESSDATA=randomstuff&amp;SKIN=default&amp;LANG=en-US">
  Change passwords
</a>

<a href="psk.exe?TRANSACTION=CGI_JUMP&amp;SESSDATA=randomstuff&amp;SKIN=default&amp;LANG=en-US">
  Unlock accounts
</a>

One link has a pss.exe and the other has psk.exe

The InnerText is "Change Password" or "Unlock Accounts"

so how can I click on the "Change Password" link. The A tag has no class or name or any easy way for me to use a css selector.


回答1:


CSS selectors are pretty versatile. You can select an element based on a part of an arbitrary attribute. So clicking the first can be achieved this way:

casper.click("a[href^='pss.exe']");

Where href^=value looks for elements with href attributes that begin with the specified value.

You can also try to use CasperJS' clickLabel function:

casper.clickLabel("Change passwords");

It sometimes doesn't work, because of whitespace.

There are of course many more ways to do this. You can for example use an XPath expression to select a link element based on its text:

casper.click(x("//a[contains(text(), 'Change passwords')]"));

with x being the XPath helper utility:

var x = require("casper").selectXPath;

If this doesn't work, then you have to make sure you are on the correct page. Take a screenshot (casper.capture(filename)) and see if you are.



来源:https://stackoverflow.com/questions/30803443/how-can-i-click-a-link-using-casperjs-without-knowing-the-css-selector

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!