简体   繁体   English

如何比较链接的名称和链接的URL(与C#中的Selenium webdriver)

[英]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. 基本上,我可以使用一些关于如何从这一点继续前进的想法。

  1. 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")); 
  2. 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.

相关问题 如何单击链接并在新选项卡中打开它 Selenium WebDriver C# - How to click a link and open it in a new tab Selenium WebDriver C# 如何在Selenium C#中打勾带有带有URL链接的文本的复选框 - How to tick a checkbox that has text with a url link in Selenium C# C#Selenium Webdriver-如何在使用字符串内引用的同时通过多个字符串查找链接? - C# Selenium Webdriver - How to find a link by multiple strings while also using an in-string reference? 如何在C#中单击Selenium的JS链接 - How to click a JS link with Selenium in C# 如何使用 selenium webdriver c# 自动发送电子邮件验证链接到为新用户注册输入的电子邮件 ID 的收件箱 - How to automate email verification link sent to inbox of the emailid entered for new user registration using selenium webdriver c# 如何使用C#使用Selenium WebDriver知道URL? - How to know a URL is existing using Selenium WebDriver using C#? 如何在 C# 中使用 Selenium WebDriver 获取当前窗口的 URL? - How to get the URL of the current window using Selenium WebDriver in C#? 如何使用Selenium Webdriver(C#)获取页面名称? - How to get a page name using Selenium Webdriver (C#)? selenium C# 中新标签页中如何打开之前复制的链接? - How to open Previously copy link in the new tab in selenium C#? 里面怎么调用link<td> 硒 c# 铬驱动程序 - How call link inside <td> selenium c# chrome driver
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM