我使用CasperJS,我希望点击具有特定长度字符串的元素( span )。例如,我想点击跨度,长度为5个字符。
Array
(
[0] => Array
(
[0] => {amdnia91(+wowa}
[1] =>
.something{property:value;prop2:value}
[2] => #something2{someprop:val;prop:val}
[3] => #something2{someprop:val;prop:val}
[4] => #something2{someprop:val;prop:val}
)
[1] => Array
(
[0] => amdnia91(+wowa
[1] => property:value;prop2:value
[2] => someprop:val;prop:val
[3] => someprop:val;prop:val
[4] => someprop:val;prop:val
)
)
有什么办法吗?
答案 0 :(得分:1)
如果要根据内容长度单击元素,则有多种方法可以实现此目的。 XPath使用public class RelayCommand<T> : ICommand
{
#region Fields
private readonly Action<T> _execute = null;
private readonly Predicate<T> _canExecute = null;
#endregion
#region Constructors
/// <summary>
/// Creates a new command that can always execute.
/// </summary>
/// <param name="execute">The execution logic.</param>
public RelayCommand(Action<T> execute)
: this(execute, null)
{
}
/// <summary>
/// Creates a new command with conditional execution.
/// </summary>
/// <param name="execute">The execution logic.</param>
/// <param name="canExecute">The execution status logic.</param>
public RelayCommand(Action<T> execute, Predicate<T> canExecute)
{
if (execute == null)
throw new ArgumentNullException("execute");
_execute = execute;
_canExecute = canExecute;
}
#endregion
#region ICommand Members
public bool CanExecute(object parameter)
{
return _canExecute == null ? true : _canExecute((T)parameter);
}
public event EventHandler CanExecuteChanged;
public void RaiseCanExecuteChanged()
{
var handler = CanExecuteChanged;
if (handler != null)
CanExecuteChanged(this, new EventArgs());
}
public void Execute(object parameter)
{
_execute((T)parameter);
}
#endregion
}
函数提供了最简单的方法。
如果元素的其中一个文本节点具有特定长度string-length(string)
:
y
如果所有内容都需要具有一定的大小(相当于之前的建议,除了单个文本节点之外没有其他节点):
casper.click(x("//span[string-length(text())=" + y + "]"));
如果空格是个问题,那么你应该删除它:
casper.click(x("//span[string-length(.)=" + y + "]"));
XPath助手通过casper.click(x("//span[string-length(normalize-space(text()))=" + y + "]"));
casper.click(x("//span[string-length(normalize-space(.))=" + y + "]"));
提供。