迭代所有“选择”;元素并在 Selenium 中获取它们的所有值

发布于 2025-01-07 01:02:14 字数 1342 浏览 0 评论 0原文

我在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

逆流 2025-01-14 01:02:14

这是 XPath 中第二常见的常见问题解答(第一个是无前缀名称和默认命名空间

在您的代码中:

options = selenium.GetSelectOptions("//select["+i+"]"); 

计算类型的表达式:

//select[position() =$someIndex]

这是以下同义词:

//select[$someIndex]

当已知 $someIndex 具有整数值,

但是,根据 // XPath 伪运算符的定义,

//select[$k]

$k 为整数时,表示:

选择中的所有select元素文档是其父级的第 $kselect 子级。”

i == 1 时,此方法有效,但当 i > 时,此方法有效。 1该方法返回null(“错误:
元素 //select[2] not found")。仅当 i == 1 时才有效。

这仅意味着在 XML 文档中不存在具有多个 select 子元素的元素。

这是一条需要记住的规则[] XPath 运算符比 // 伪运算符具有更高的优先级(优先级)

解决方案 。 :一如既往,当我们需要覆盖默认优先级时对于运算符,我们必须使用括号

更改

options = selenium.GetSelectOptions("//select["+i+"]");               

更改为

options = selenium.GetSelectOptions("(//select)["+i+"]");   

This is the second most FAQ in XPath (the first being unprefixed names and default namespace.

In your code:

options = selenium.GetSelectOptions("//select["+i+"]"); 

An expression of the type is evaluated:

//select[position() =$someIndex]

which is a synonym for:

//select[$someIndex]

when it is known that $someIndex has an integer value.

However, by definition of the // XPath pseudo-operator,

//select[$k]

when $k is integer, means:

"Select all select elements in the document that are the $k-th select child of their parent."

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.

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:

options = selenium.GetSelectOptions("//select["+i+"]");               

to:

options = selenium.GetSelectOptions("(//select)["+i+"]");   
樱&纷飞 2025-01-14 01:02:14

最后我找到了解决方案。
我刚刚

options = selenium.GetSelectOptions("//select["+i+"]");
selenium.Select("//select["+i+"]", "label="+label);

用这些替换了这些行

options = selenium.GetSelectOptions("//descendant::select[" + i + "]");
selenium.Select("//descendant::select[" + i + "]", "label=" + label);

Finally I've found a solution.
I've just replaced these lines

options = selenium.GetSelectOptions("//select["+i+"]");
selenium.Select("//select["+i+"]", "label="+label);

with these

options = selenium.GetSelectOptions("//descendant::select[" + i + "]");
selenium.Select("//descendant::select[" + i + "]", "label=" + label);
烟酒忠诚 2025-01-14 01:02:14

上面的解决方案 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 used css=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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文