Selenium C# Webdriver FindElements(By.LinkText) RegEx?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12447690/
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
Selenium C# Webdriver FindElements(By.LinkText) RegEx?
提问by Sam
Is it possible to find links on a webpage by searching their text using a pattern like A-ZNN:NN:NN:NN, where Nis a single digit (0-9).
是否可以通过使用类似的模式搜索文本来查找网页上的链接A-ZNN:NN:NN:NN,其中N是一位数字 (0-9)。
I've used Regex in PHP to turn text into links, so I was wondering if it's possible to use this sort of filter in Selenium with C# to find links that will all look the same, following a certain format.
我已经在 PHP 中使用 Regex 将文本转换为链接,所以我想知道是否可以在 Selenium 中使用这种过滤器和 C# 来查找看起来都一样的链接,遵循某种格式。
I tried:
我试过:
driver.FindElements(By.LinkText("[A-Z][0-9]{2}):([0-9]{2}):([0-9]{2}):([0-9]{2}")).ToList();
But this didn't work. Any advice?
但这没有用。有什么建议吗?
采纳答案by JimEvans
In a word, no, none of the FindElement()strategies support using regular expressions for finding elements. The simplest way to do this would be to use FindElements()to find all of the links on the page, and match their .Textproperty to your regular expression.
总之,不,没有一种FindElement()策略支持使用正则表达式来查找元素。最简单的方法是使用FindElements()查找页面上的所有链接,并将它们的.Text属性与您的正则表达式匹配。
Note though that if clicking on the link navigates to a new page in the same browser window (i.e., does not open a new browser window when clicking on the link), you'll need to capture the exact text of all of the links you'd like to click on for later use. I mention this because if you try to hold onto the references to the elements found during your initial FindElements()call, they will be stale after you click on the first one. If this is your scenario, the code might look something like this:
请注意,如果单击链接导航到同一浏览器窗口中的新页面(即,单击链接时不会打开新的浏览器窗口),则您需要捕获所有链接的确切文本想点击以后使用。我之所以提到这一点,是因为如果您尝试保留对在初始FindElements()调用期间找到的元素的引用,则在您单击第一个元素后它们将失效。如果这是您的场景,则代码可能如下所示:
// WARNING: Untested code written from memory.
// Not guaranteed to be exactly correct.
List<string> matchingLinks = new List<string>();
// Assume "driver" is a valid IWebDriver.
ReadOnlyCollection<IWebElement> links = driver.FindElements(By.TagName("a"));
// You could probably use LINQ to simplify this, but here is
// the foreach solution
foreach(IWebElement link in links)
{
string text = link.Text;
if (Regex.IsMatch("your Regex here", text))
{
matchingLinks.Add(text);
}
}
foreach(string linkText in matchingLinks)
{
IWebElement element = driver.FindElement(By.LinkText(linkText));
element.Click();
// do stuff on the page navigated to
driver.Navigate().Back();
}
回答by Anirudha
Dont use regex to parse Html.
不要使用正则表达式来解析 Html。
Use htmlagilitypack
You can follow these steps:
您可以按照以下步骤操作:
Step1Use HTML PARSERto extract all the links from the particular webpage and store it into a List.
Step1用于HTML PARSER从特定网页中提取所有链接并将其存储到列表中。
HtmlWeb hw = new HtmlWeb();
HtmlDocument doc = hw.Load(/* url */);
foreach(HtmlNode link in doc.DocumentElement.SelectNodes("//a[@href]"))
{
//collect all links here
}
Step2Use this regex to match all the links in the list
Step2使用这个正则表达式匹配列表中的所有链接
.*?[A-Z]\d{2}:\d{2}:\d{2}:\d{2}.*?
Step 3You get your desired links.
第 3 步您将获得所需的链接。

