简体   繁体   English

Selenium C# 使用 class 和文本查找元素

[英]Selenium C# Find Element with class and text

im very new to testing and have no training in automated tests so please bare with me if i say stupid things but ill try the best i can.我对测试非常陌生,并且没有接受过自动化测试的培训,所以如果我说一些愚蠢的话,请与我坦诚相待,但我会尽力而为。

Bascially i am trying to assert that a specific employee in the employee list has the status of 'leaver'.基本上,我试图断言员工列表中的特定员工具有“离职者”状态。

This is what i have tried (and other variations with the different classes)这是我尝试过的(以及不同类别的其他变体)

Assert.Equal("image-tile__badge background-color--status-leaver ng-star-inserted", Driver.FindElement(By.XPath("//*[contains(@class,'image-tile__content-header') and contains(text(),'End Date, Contract') and contains(@class, 'image-tile__badge')]")).GetAttribute("Class"));

Assert.Equal("image-tile__badge background-color--status-leaver ng-star-inserted", Driver.FindElement(By.XPath("//*[contains(@class,'image-tile__content-header') and contains(text(),'End Date, Contract')]")).FindElement(By.XPath("//*[contains(@class, 'image-tile__badge')]")).GetAttribute("Class"));
       

The last one finds the element when the status is 'new', but when i change the employee status to 'leaver', it still returns as 'new' so possibly looking at another employee with a 'new' status.最后一个在状态为“新”时找到元素,但是当我将员工状态更改为“离职者”时,它仍然返回为“新”,因此可能正在查看另一个具有“新”状态的员工。

Hopefully this is enough info, let me know if more is needed (this is my first ever post!) HTML code in image below希望这是足够的信息,如果需要更多信息,请告诉我(这是我的第一篇文章!)下图中的 HTML 代码

[HTML code on Chrome] [1]: https://i.stack.imgur.com/kUxkf.png [Chrome 上的 HTML 代码] [1]: https://i.stack.imgur.com/kUxkf.png

Summary: im trying to assert that the Employee "End Date, Contract" has the status of leaver (aka the leaver class "image-tile__badge background-color--status-leaver ng-star-inserted")摘要:我试图断言员工“结束日期,合同”具有离开者的状态(又名离开者 class“image-tile__badge background-color--status-leaver ng-star-inserted”)

Thanks everyone for their help: One of my devs managed to take @noldors example and modify it a bit so heres what ended up working for me:感谢大家的帮助:我的一位开发人员设法以@noldors 为例并对其进行了一些修改,因此最终对我有用:

 var newElmList1 = Driver.FindElements(By.CssSelector("div.background-color--status-leaver")).ToList();
            List<string> newNames1 = new List<string>();
            foreach (var newElm in newElmList1)
            {
                var newName1 = newElm.FindElement(By.XPath(".."))
                                    .FindElement(By.CssSelector("div.image-tile__content-header")).Text;
                newNames.Add(newName1);
            }
            if (!newNames.Contains("End Date, Contract"))
            {
                throw new Exception("Exception Error on leaver Person");
            }

According to your screenshot, you can find all elements with 'Leaver' specific class with this;根据您的屏幕截图,您可以找到所有带有“Leaver”特定 class 的元素;

 var leaverElmList = Driver.FindElements(By.CssSelector("div.background-color--status-leaver")).ToList();
 List<string> leaverNames = new List<string>();
 foreach (var leaverElm in leaverElmList) {
    var leaverName = leaverElm.FindElement(By.XPath(".."))
                        .FindElement(By.CssSelector("div.image-tile__content-header"));
                        .Text()
    leaverNames.Add(leaverName);
 }

As per your screenshot i fill it's better if you try using Xpath根据您的屏幕截图,如果您尝试使用 Xpath,我会更好

var elmList = Driver.FindElements(By.Xpath("//div[contains(text(),'leaver')]")).ToList();

i hope it will help you Thank You.我希望它会帮助你谢谢。

Enddate, Contract which is not related to the div that contains Leaver. Enddate,与包含 Leaver 的 div 无关的 Contract。 It's direct parent is the image-tile div它的直接父级是图像平铺 div

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM