Selenium C#Webdriver FindElements(By.LinkText)正则表达式? [英] Selenium C# Webdriver FindElements(By.LinkText) RegEx?

查看:118
本文介绍了Selenium C#Webdriver FindElements(By.LinkText)正则表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过使用 A-ZNN:NN:NN:NN 这样的模式搜索文本来查找网页上的链接,其中 N 是一位数字(0-9).

我已经在PHP中使用Regex将文本转换为链接,所以我想知道是否可以在Selenium中将这种过滤器与C#一起使用,以某种格式查找看起来相同的链接.

我尝试过:

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

但这没用.有什么建议吗?

总之,不,没有 FindElement()策略不支持使用正则表达式来查找元素.最简单的方法是使用 FindElements()查找页面上的所有链接,并将其 .Text 属性与您的正则表达式匹配.

请注意,如果单击链接会在同一浏览器窗口中导航到新页面(即,单击链接时未打开新的浏览器窗口),则需要捕获所有您想要单击以供以后使用的链接.我之所以这样说是因为,如果您尝试保留对在最初的 FindElements()调用期间找到的元素的引用,则在单击第一个元素之后它们将失效.如果是这种情况,代码可能看起来像这样:

 //警告:未测试的代码是从内存写入的.//不保证完全正确.List< string>matchingLinks = new List< string>();//假设驱动程序"是有效的IWebDriver.ReadOnlyCollection< IWebElement>链接= driver.FindElements(By.TagName("a"));//您可能可以使用LINQ简化此过程,但这是//foreach解决方案foreach(链接中的IWebElement链接){字符串文本= link.Text;如果(Regex.IsMatch(此处为您的Regex",文本)){matchingLinks.Add(text);}}foreach(matchingLinks中的字符串linkText){IWebElement元素= driver.FindElement(By.LinkText(linkText));element.Click();//将页面上的内容导航到driver.Navigate().Back();} 

Is it possible to find links on a webpage by searching their text using a pattern like A-ZNN:NN:NN:NN, where N is a single digit (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.

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?

解决方案

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 .Text property to your regular expression.

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:

// 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();
}

这篇关于Selenium C#Webdriver FindElements(By.LinkText)正则表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆