在 C# 中使用 Selenium 通过部分 id 查找元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11961178/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 19:51:43  来源:igfitidea点击:

Finding an element by partial id with Selenium in C#

c#regexselenium

提问by Michael Howell

I am trying to locate an element with a dynamically generated id. The last part of the string is constant ("ReportViewer_fixedTable"), so I can use that to locate the element. I have tried to use regex in XPath:

我正在尝试使用动态生成的 id 定位元素。字符串的最后一部分是常量(“ReportViewer_fixedTable”),因此我可以使用它来定位元素。我曾尝试在 XPath 中使用正则表达式:

targetElement = driver.FindElement(
    By.XPath("//table[regx:match(@id, "ReportViewer_fixedTable")]"));

And locating by CssSelector:

并通过 CssSelector 定位:

targetElement = driver.FindElement(
    By.CssSelector("table[id$='ReportViewer_fixedTable']"));

Neither works. Any suggestions would be appreciated.

两者都不起作用。任何建议,将不胜感激。

采纳答案by Greg

That is because the css selector needs to be modified you were almost there...

那是因为 css 选择器需要修改你几乎就在那里......

driver.FindElement(By.CssSelector("table[id*='ReportViewer_fixedTable']"))`

From https://saucelabs.com/blog/selenium-tips-css-selectors-in-selenium-demystified:

来自https://saucelabs.com/blog/selenium-tips-css-selectors-in-selenium-demystified

css=a[id^='id_prefix_']

A link with an idthat starts with the text id_prefix_.

id以文本开头的链接id_prefix_

css=a[id$='_id_sufix']

A link with an idthat ends with the text _id_sufix.

id以文本 结尾的链接_id_sufix

css=a[id*='id_pattern']

A link with an idthat contains the text id_pattern.

带有id包含文本 的链接id_pattern

You were using a suffix which I'm assuming was not the partial link text identifier you were supposed to be using (unless I saw your html, which means try showing your html next time). *=is reliable in any situation though.

您使用的后缀我假设不是您应该使用的部分链接文本标识符(除非我看到了您的 html,这意味着下次尝试显示您的 html)。*=不过在任何情况下都是可靠的。

回答by Prashant Shukla

try using

尝试使用

targetElement = driver.FindElement(By.XPath("//table[contains(@id, "ReportViewer_fixedTable")]"));

Note this will check for all the elements that have id which contains (and not only ends with 'ReportViewer_fixedTable'). I will try to find a regex option that would be more accurate answer to you question.

请注意,这将检查所有包含 id 的元素(并且不仅以“ReportViewer_fixedTable”结尾)。我将尝试找到一个正则表达式选项,可以更准确地回答您的问题。

回答by iSunilSV

This solution will work irrespective of the XPath version. First, create a method somewhere in your COMMON helper class.

无论 XPath 版本如何,此解决方案都将起作用。首先,在您的 COMMON 帮助器类中的某处创建一个方法。

 public static string GetXpathStringForIdEndsWith(string endStringOfControlId)
    {
        return "//*[substring(@id, string-length(@id)- string-length(\"" + endStringOfControlId + "\") + 1 )=\"" + endStringOfControlId + "\"]";
    }

In my case, below is the control ID in different version of my product ::

就我而言,以下是我产品不同版本中的控件 ID ::

v1.0 :: ContentPlaceHolderDefault_MasterPlaceholder_HomeLoggedOut_7_hylHomeLoginCreateUser

v1.0 :: ContentPlaceHolderDefault_MasterPlaceholder_HomeLoggedOut_7_hylHomeLoginCreateUser

v2.0 :: ContentPlaceHolderDefault_MasterPlaceholder_HomeLoggedOut_8_hylHomeLoginCreateUser

v2.0 :: ContentPlaceHolderDefault_MasterPlaceholder_HomeLoggedOut_8_hylHomeLoginCreateUser

Then, you can call the above method to find the control which has static end string.

然后,您可以调用上述方法来查找具有静态结束字符串的控件。

By.XPath(Common.GetXpathStringForIdEndsWith("<End String of the Control Id>"))

For the control ID's which I mentioned for v1 & v2, I use like below :

对于我在 v1 和 v2 中提到的控件 ID,我使用如下:

By.XPath(Common.GetXpathStringForIdEndsWith("hylHomeLoginCreateUser"))

The overall logic is that, you can use the below XPath expression to find a control which ends with particular string:

总体逻辑是,您可以使用以下 XPath 表达式来查找以特定字符串结尾的控件:

//*[substring(@id, string-length(@id)- string-length("<EndString>") + 1 )="<EndString>"]