C# selenium一个接一个地点击几个链接
我在网页上有一个表,其中有许多重复的值,如下所示:C# selenium一个接一个地点击几个链接,c#,selenium,webdriver,selenium-webdriver,C#,Selenium,Webdriver,Selenium Webdriver,我在网页上有一个表,其中有许多重复的值,如下所示: 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
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
在我的应用程序开始时,它会要求用户输入他们选择的应用程序名。我想要的是,如果我选择APP2,它应该首先选择一些Desc2,这将导致另一个页面,在那里我会做一些事情。然后它应该再次返回到上一页,这次它应该选择一些Desc3,这将导致另一页。这应该重复n次,直到selenium找不到指定的appname
我已尝试如下所示:
//Finding Table, its rows and coloumns
int rowcount = driver.FindElements(By.Id("someid")).Count;
for (int i = 0; i < rowcount; i++)
{
//Finding App name based on user entered text
var elems = driver.FindElements(By.PartialLinkText(text));
IList<IWebElement> list = elems;
for (int j = 0; j < list.Count; j++)
{
var table = driver.FindElement(By.Id("someid"));
IList<IWebElement> rows = table.FindElements(By.TagName("tr"));
IList<IWebElement> cells = rows[i].FindElements(By.TagName("td"));
//Again finding element based on user entered text
var elem = driver.FindElements(By.PartialLinkText(text));
list = elem;
if (list[1].Text.Equals(text))
{
list[0].Click();
string duration;
string price;
var elements = driver.FindElements(By.Id("SPFieldNumber"));
IList<IWebElement> lists = elements;
duration = lists.First().Text.ToString();
price = lists.ElementAt(1).Text.ToString();
MessageBox.Show(duration);
MessageBox.Show(price);
driver.Navigate().Back();
}
}
}
运行这段代码可以正确地选择一些Desc2,并且一切正常。但返回到上一页后,c抛出缓存中未找到的异常元素-可能该页在查找后已更改。对于此特定问题,您可以在循环之前找到表和行元素,然后调用driver.Navigate.Back;在循环内部,您的表和行不再位于DOM中,因为页面发生了变化,DOM发生了变化,表元素不再是循环外部的元素 试着把它们放在循环中
int rowCount = driver.FindElements(By.CssSelector("#table_id tr")).Count; // replace table_id with the id of your table
for (int i = 0; i < rowCount ; i++)
{
var table = driver.FindElement(By.Id("some ID"));
rows = table.FindElements(By.TagName("tr"));
// the rest of the code
}
如果没有框架,你就不需要这个。
driver.SwitchTo.DefaultContent;
在您之前的文章中,IWebElement的列表永远不会等于字符串,并且结果不能是元素。如果你不知道你想要什么类型,避免使用var,因为它可能会给你带来完全不同的东西。
IList列表=元素;
var elem=list.Equalstext;
与前面的post element.ToString和element.Text不同
字符串targetele=elem.ToString;//你想要元素文本;
这将在哪一行抛出?@Arran:if列表[1]之后。Text.Equalstext{你还在这个问题上吗?天哪…我在你之前问的问题中向你展示了如何处理“过时元素异常”。你不使用for WEbElemet元素:元素样式循环。你需要为I=0使用计数器;i@MarkRowlands:实际上我忘了。我是这样更改的。我将其更改为int j=0;j