Selenium C # Webdriver FindElements (By.LinkText) RegEx?

Is it possible to find links on a web page by searching for their text using a type template A-ZNN:NN:NN:NN, where Nis one digit (0-9).

I used Regex in PHP to convert text to links, so I was wondering if it is possible to use this filter in Selenium with C # to find links that will look the same, following a specific format.

I tried:

driver.FindElements(By.LinkText("[A-Z][0-9]{2}):([0-9]{2}):([0-9]{2}):([0-9]{2}")).ToList();

But that did not work. Any advice?

+5
source share
2 answers

, , FindElement() . - FindElements(), .Text .

, (.. ), , . , , 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();
}
+7

Html.

htmlagilitypack

:

1 HTML PARSER, - .

HtmlWeb hw = new HtmlWeb();
 HtmlDocument doc = hw.Load(/* url */);
 foreach(HtmlNode link in doc.DocumentElement.SelectNodes("//a[@href]"))
 {
//collect all links here
 }

Step2

.*?[A-Z]\d{2}:\d{2}:\d{2}:\d{2}.*?

3. .

+1

All Articles