循环浏览 Web 元素并单击每个链接 [英] Loop through Web Elements and Click each link
问题描述
我的代码有效我的目标是遍历表格中的每个元素并单击该元素,以便我可以转到下一页基本上我想这样做
1 单击元素表单下拉列表2//编码一些东西3 回去4 单击下拉列表中的下一个元素5 编码一些东西等
我的代码:
ListCCTable = driver.findElements(By.id("companyIdBarCompListGrid_rows_scrollpane"));for ( WebElement 客户端:CCTable) {System.out.println("\n"+client.getText().substring(0, 20)+"\n");客户端点击();线程睡眠(10000);}
我的问题是它在我的 **console in eclipse ** 中查找并打印文本,但它没有点击链接或每个链接.
我正在尝试遵循这里的建议
似乎获取文本未获取其他公司代码名称.它在点击名字后不再循环,我点击我得到org.openqa.selenium.StaleElementReferenceException:过时的元素引用:元素未附加到页面文档.
这是一个工作循环:
String selector = "div#companyIdBarCompListGrid_rows_scrollpane table tbody tr[id*=companyIdBarCompListGrid_] td span div a";int links = driver.findElements(By.cssSelector(selector)).size();System.out.println("链接数:" + links);for (int i = 0; i < 链接; i++) {列表CCTable = driver.findElements(By.cssSelector(selector));WebElement 客户端 = CCTable.get(i);System.out.println("\n"+client.getText().substring(0, 20)+"\n");客户端点击();//事情发生在这里}
Hi my code works My goal is to loop through each element in the table and and click the element so i can go to the next page basically I want to do this
1 click element form drop down
2 //code some stuff
3 Go back
4 click next element from drop down
5 code some stuff and etc
My code:
List<WebElement> CCTable = driver.findElements(By.id("companyIdBarCompListGrid_rows_scrollpane"));
for ( WebElement client: CCTable) {
System.out.println("\n"+client.getText().substring(0, 20)+"\n");
client.click();
Thread.sleep(10000);
}
My problem is that its finding and printing out the text in my **console in eclipse ** but it is not clicking on the link or each link.
I am trying to follow the suggestions here https://sqa.stackexchange.com/questions/12790/how-to-iterate-over-a-collection-of-items-in-selenium-webdriver but no success any help would be appreciated.
If I am doing something wrong please help me. Any help would be appreciated
PP_OBJ_CycleData.CCdropdown(driver).click();
List<WebElement> CCTable = driver.findElements(By.cssSelector("div#companyIdBarCompListGrid_rows_scrollpane table tbody tr[id*=companyIdBarCompListGrid_] td span div a"));
// inner 4 loop
for ( WebElement ccode: CCTable) {
System.out.println("\n"+ccode.getText().substring(0, 20)+"\n");
System.out.println("Number of links: " + CCTable.size());
Thread.sleep(3000);
ccode.click();
Thread.sleep(5000);
driver.findElement(By.xpath("//*[@id='companyIDBarContentPane']//span"));
}// End Inner 4 loop
PP_OBJ_CycleData.ReturnToSupport(driver);
Also this is in eclipse console:
Seems that the get text is not getting the other company code name. and it not looping through again after it clicks the first name and i click i get org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document.
Here is a working loop:
String selector = "div#companyIdBarCompListGrid_rows_scrollpane table tbody tr[id*=companyIdBarCompListGrid_] td span div a";
int links = driver.findElements(By.cssSelector(selector)).size();
System.out.println("Number of links: " + links);
for (int i = 0; i < links; i++) {
List<WebElement> CCTable = driver.findElements(By.cssSelector(selector));
WebElement client = CCTable.get(i);
System.out.println("\n"+client.getText().substring(0, 20)+"\n");
client.click();
// Things happen here
}
这篇关于循环浏览 Web 元素并单击每个链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!