[英]How to compare name of link with URL of link (with Selenium webdriver in C#)
I want to automate the testing of a Sharepoint site, to see if the links are leading to the right URL. 我想自动测试Sharepoint站点,看看链接是否通向正确的URL。 Right now I am stuck on how to do the comparison.
现在我被困在如何进行比较。
I have located the links and put them in a list. 我找到了链接并将它们放在一个列表中。 I am new to Selenium so I have been trying everything I could think of/Google in the if-statement.
我是Selenium的新手,所以我在if语句中尝试了一切我能想到的/ Google。
IWebElement tableElement = driver.FindElement(By.XPath("//table[@id='skills-table']"));
IList<IWebElement> tableRow = tableElement.FindElements(By.TagName("a"));
String[] rowTD = new String[tableRow.Count];
int i = 0;
foreach (IWebElement element in tableRow)
{
rowTD[i++] = element.Text;
if (element.Text.Equals ) // Some kind of comparison
{
}
}
Basically, I could just use some ideas on how to move on from this point. 基本上,我可以使用一些关于如何从这一点继续前进的想法。
I don't think your tableElement.FindElements(By.TagName("a"));
我不认为你的
tableElement.FindElements(By.TagName("a"));
expression will return you all the nested links in the table, personally I would rather go for a relative XPath locator like: 表达式将返回表中的所有嵌套链接,我个人更愿意找一个相对的XPath定位器,如:
IList<IWebElement> tableRow = tableElement.FindElements(By.XPath(".//a"));
If you want to get URL of the link you need to extract href
attribute via IWebElement.GetAttribute() function 如果你想获得链接的URL,你需要通过IWebElement.GetAttribute()函数提取
href
属性
var URL = element.GetAttribute("href");
Thanks a lot for the answer. 非常感谢你的回答。 I might have figured it out, and could use some comments on the solution.
我可能已经弄明白了,可以对解决方案使用一些评论。 Is Assert.ReferenceEquals solving my problem?
Assert.ReferenceEquals解决了我的问题吗? The test passes atleast
测试通过至少
IWebElement tableElement = driver.FindElement(By.XPath("//table[@id='skills-table']"));
IList<IWebElement> tableRow = tableElement.FindElements(By.XPath(".//a"));
String[] rowTD = new String[tableRow.Count];
int i = 0;
foreach (IWebElement element in tableRow)
{
rowTD[i++] = element.Text;
var URL = element.GetAttribute("href");
Assert.ReferenceEquals(element.Text, URL);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.