找到所有"A".在属性href中带有特定字符串的标签? [英] Finding all "A" tags with specific strings in the attribute href?

查看:74
本文介绍了找到所有"A".在属性href中带有特定字符串的标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

            driver.FindElement(By.Name("zipcode")).Clear();
            driver.FindElement(By.Name("zipcode")).SendKeys(zipcode);
            driver.FindElement(By.Name("Go")).Click();

            driver.FindElements(By.TagName("A").  //<---- ?????????

我有一些开始的Selenium API代码.我的目标是在属性href中获得所有带有字符串"alertsepy"和字符串"sevendwarves"的"A"标签,并将所有这些元素返回到数组中,以便进行进一步的处理.我开始编写代码,但实际上我还不太确定如何将所有方法都做到这一点.有谁知道如何使用Selenium进行此类查询.

I have some Selenium API code that I started. I aim to get all the "A" tags with the string "alertsepy" and the sting "sevendwarves" in the attribute href and return all those elements into an array so I can do some further processing. I started the code but I am really not quite sure how to get all the way there yet. Does anyone know how to do this type of query with Selenium.

亲切的问候!

推荐答案

您应使用CSS选择器:

You should use css selector:

IList<IWebElement> elements = driver.findElements(By.cssSelector("a[href*=alertsepy],a[href*=sevendwarves]")

此查询将返回具有href属性的a个节点,该属性包含 alertsepy sevendwarves 两个字符串:

This query will return a nodes with href attribute that contains alertsepy or sevendwarves or both strings:

<a href="alertsepy.html" > </a>
<a href="sevendwarves.html" > </a>
<a href="http://sevendwarves.org/alertsepy.html" > </a>

或者您可以使用:

IList<IWebElement> elements = driver.findElements(By.cssSelector("a[href*=alertsepy][href*=sevendwarves]")    

此查询将返回具有href属性的a个节点,其中包含 alertsepy sevendwarves 字符串:

This query will return a nodes with href attribute that contains alertsepy and sevendwarves strings:

<a href="http://sevendwarves.org/alertsepy.html" > </a>

有关一般可用的css选择器的列表,请参考 w3c css选择器.有关Selenium查询类型中可用的列表,请参考定位UI元素.

For a list of generally available css selectors refer to w3c css selectors. For the list of available in Selenium query types refer to Locating UI Elements.

这篇关于找到所有"A".在属性href中带有特定字符串的标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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