本文介绍了如何在不知道css选择器的情况下使用CasperJS单击链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

一个链接有pss.exe,另一个链接有psk.exe

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

InnerText是更改密码或解锁帐户

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.

推荐答案

CSS选择器是非常灵活的。您可以根据任意属性的一部分选择元素。所以点击第一个可以这样实现:

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']");

其中 href ^ = value

您也可以尝试使用CasperJS的clickLabel函数:

You can also try to use CasperJS' clickLabel function:

casper.clickLabel("Change passwords");

有时不能使用空格。

当然还有更多的方法。例如,您可以使用XPath表达式根据其文本选择链接元素:

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')]"));

x

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

如果这不起作用,那么你必须确保你在正确的页面。取一个截图( casper.capture(filename)),看看是否是。

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.

这篇关于如何在不知道css选择器的情况下使用CasperJS单击链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 09:17
查看更多