最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - how can I click a link using CasperJS without knowing the css selector - Stack Overflow

matteradmin4PV0评论
<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.

<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.

Share Improve this question edited Jun 12, 2015 at 12:49 Artjom B. 62k26 gold badges135 silver badges230 bronze badges asked Jun 12, 2015 at 12:45 GettingStartedGettingStarted 7,63521 gold badges80 silver badges152 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 11

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.

Post a comment

comment list (0)

  1. No comments so far