0

我在我的 c# selenium 应用程序中有另一个这样的要求。由于我的网页表格中有很多内容,因此根据应用名称对其进行排序会非常好。我有这样的内容:

Description     App Name    Information
Some Desc1       App1         Some Info
Some Desc2       App2         Some Info
Some Desc3       App2         Some Info
Some Desc4       App3         Some Info
Some Desc5       App4         Some Info

在应用程序名称旁边,我有一个列出应用程序名称的排序按钮。我可以单击排序按钮,我可以查看所有这些应用程序名称。但我无法单击我需要的特定应用程序名称。

我尝试过这样的事情:

var elems = driver.FindElements(By.TagName("SPAN")); //SPAN is the only attribute I could see and it is also unique for all the app names provided in the list. So, used driver.FindElements
IList<IWebElement> list = elems; //Converts it as a list
var elem= list.Equals(text); // text represents an app name provided by the user in a textbox at the start of the application. Storing that appname into a variable elem.
string targetele = elem.ToString(); //Converting it into a string
if (list.Equals(text))
{
   driver.FindElement(By.Name(targetele)).Click(); //Click on a particular app name to filter
}
4

1 回答 1

3

首先,为了说明您的问题,HTML 代码比显示业务模型要简洁得多。如果可能,请发布它。

现在对于这个问题,您到底要做什么?我只能根据您提供的代码发表评论。

// are you sure want to get all spans? This will get you lots more unrelated ones
// please try use xpath or css selector to only get the ones related
IList<IWebElement> spanList = driver.FindElements(By.TagName("span"));

// now you want loop through each of them, check if text == App4 as an example
foreach (IWebElement span in spanList) {
    if (span.Text == "App4") {
        // click the span with text App4
        span.Click(); // not sure what you want to click here, please clarify
    }
}

// version using Linq
// spanList.First(span => span.Text == "App4").Click();
于 2013-08-07T06:13:17.363 回答