迭代所有“选择”;元素并在 Selenium 中获取它们的所有值
我在使用 selenium 的 C# 中有以下代码:
private void SelectElementFromList(string label)
{
var xpathcount = selenium.GetXpathCount("//select");
for (int i = 1; i <= xpathcount; ++i)
{
string[] options;
try
{
options = selenium.GetSelectOptions("//select["+i+"]");
}
catch
{
continue;
}
foreach (string option in options)
{
if (option == label)
{
selenium.Select("//select[" + i + "]", "label=" + label);
return;
}
}
}
}
问题是行:
options = selenium.GetSelectOptions("//select["+i+"]");
When i == 1 this Works, but when i > 1 该方法返回 null(“错误:元素 //select[2] 未找到”)。它仅在 i == 1 时有效。
我也在 JS 中尝试过此代码:
var element = document.evaluate("//select[1]/option[1]/@value", document, null, XPathResult.ANY_TYPE, null);
alert(element.iterateNext());
var element = document.evaluate("//select[2]/option[1]/@value", document, null, XPathResult.ANY_TYPE, null);
alert(element.iterateNext());
在屏幕上打印“[object Attr]”,然后“null”。
我做错了什么? 我的目标是迭代页面上的所有“选择”元素,找到具有指定标签的元素并选择它。
I have the following code in C# using selenium:
private void SelectElementFromList(string label)
{
var xpathcount = selenium.GetXpathCount("//select");
for (int i = 1; i <= xpathcount; ++i)
{
string[] options;
try
{
options = selenium.GetSelectOptions("//select["+i+"]");
}
catch
{
continue;
}
foreach (string option in options)
{
if (option == label)
{
selenium.Select("//select[" + i + "]", "label=" + label);
return;
}
}
}
}
The problem is the line:
options = selenium.GetSelectOptions("//select["+i+"]");
When i == 1 this works, but when i > 1 the method return null ("ERROR: Element //select[2] not found"). It works only when i == 1.
I have also tried this code in JS:
var element = document.evaluate("//select[1]/option[1]/@value", document, null, XPathResult.ANY_TYPE, null);
alert(element.iterateNext());
var element = document.evaluate("//select[2]/option[1]/@value", document, null, XPathResult.ANY_TYPE, null);
alert(element.iterateNext());
Which print on the screen "[object Attr]" and then "null".
What am I doing wrong?
My goal is to iterate all "select" elements on the page and find the one with the specified label and select it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是 XPath 中第二常见的常见问题解答(第一个是无前缀名称和默认命名空间。
在您的代码中:
计算类型的表达式:
这是以下同义词:
当已知
$someIndex
具有整数值,但是,根据
//
XPath 伪运算符的定义,当
$k
为整数时,表示:“选择中的所有
select
元素文档是其父级的第$k
个select
子级。”这仅意味着在 XML 文档中不存在具有多个
select
子元素的元素。这是一条需要记住的规则:
[]
XPath 运算符比//
伪运算符具有更高的优先级(优先级)解决方案 。 :一如既往,当我们需要覆盖默认优先级时对于运算符,我们必须使用括号
更改:
更改为:
This is the second most FAQ in XPath (the first being unprefixed names and default namespace.
In your code:
An expression of the type is evaluated:
which is a synonym for:
when it is known that
$someIndex
has an integer value.However, by definition of the
//
XPath pseudo-operator,when
$k
is integer, means:"Select all
select
elements in the document that are the$k
-thselect
child of their parent."This simply means that in the XML document there is no element that has more than one
select
child.This is a rule to remember: The
[]
XPath operator has higher precedence (priority) than the//
pseudo-operator.The solution: As always when we need to override the default precedence of operators, we must use brackets.
Change:
to:
最后我找到了解决方案。
我刚刚
用这些替换了这些行
Finally I've found a solution.
I've just replaced these lines
with these
上面的解决方案
options = selenium.GetSelectOptions("(//select)["+i+"]");
对我不起作用,但我尝试使用 css 选择器。我想获取用户名和密码文本框。我尝试使用
css=input
这给了我用户名文本框,当使用css=input+input
这给了我密码文本框。与此选择器一起,您可以组合使用许多东西。
这里是我阅读的链接。
我认为这将帮助你实现你的目标。
问候。
The above solution
options = selenium.GetSelectOptions("(//select)["+i+"]");
doesn't worked for me but i tried to use css selectors.I want to get username and password text box. I tried with
css=input
this gave me Username text box and when usedcss=input+input
this gave me Password textbox.along with this selectors you can use many things in combination.
here is the link from where i read.
I think this will help u to achieve your target.
Regards.