Java 遍历所有行并单击表中每次迭代中每行的第二个单元格
这是我的密码 //抓住桌子 WebElement table=driver.findElementBy.idAccount\u body; //现在从表中获取所有TR元素 列出所有行=table.findElementsBy.tagNametr; //然后迭代它们,得到细胞 对于WebElement行:allRows{ List cells=row.findElementsBy.xpath//表[@class='List']//td[normalize space='Edit']/以下同级::th/a; 对于WebElement单元格:单元格{ //等等 cell.click;Java 遍历所有行并单击表中每次迭代中每行的第二个单元格,java,selenium,Java,Selenium,这是我的密码 //抓住桌子 WebElement table=driver.findElementBy.idAccount\u body; //现在从表中获取所有TR元素 列出所有行=table.findElementsBy.tagNametr; //然后迭代它们,得到细胞 对于WebElement行:allRows{ List cells=row.findElementsBy.xpath//表[@class='List']//td[normalize space='Edit']/以下同级::
System.out.printlnclicked row;假设您的代码编写正确,并且在正确的位置关闭“}”,下面是一些可能会有所帮助的想法 首先,我看不到table元素的id属性,因此如果这实际上得到了正确的元素,我会感到惊讶
WebElement table = driver.findElement(By.id("Account_body"));
如果这不是正确的元素,那么很可能:
List<WebElement> allRows = table.findElements(By.tagName("tr"));
它应该看起来更像:
By.xpath("./td")
因为行的位置已经在您正在引用的表元素下
第三,如果您只单击每行的第二列,并且我希望您实际上对单击第一行不感兴趣,因为它们只是标题,那么您不应该使用第二个foreach循环
第四,这真的是你感兴趣的第二栏,还是你感兴趣的客户名称栏
我可以想象这样的东西会起作用,尽管您可能需要稍微使用XPath,因为我没有尝试实际插入XPath以查看它们是否匹配元素
WebElement table = driver.findElement(By.xpath(".//table[@class='list']"));
// Now get all the TR elements from the table but excludes the header row
List<WebElement> allRows = table.findElements(By.xpath(".//tr[not(@class='headerRow')"));
// And iterate over them, getting the cells
for (WebElement row : allRows) {
//assuming that you are actually trying to click on the link for each user name,
//and not just the second element
WebElement cell = row.findElement(By.xpath("./th/a");
// And so on
cell.click();
老实说,虽然我不知道这是否能解决你的问题。如果没有任何关于为什么你不能点击第二列的信息,这只是猜测。如果你能同时包括代码失败的地方和异常错误,这将允许其他人给出更好的答案。好的,完成了。下一步怎么办?-说真的,你做了什么e到目前为止您已经尝试过了,失败在哪里?欢迎使用Stack Overflow。我建议您选择一个。@Thomas我无法在每次迭代中单击行中的第二个元素。我已经添加了代码
WebElement table = driver.findElement(By.xpath(".//table[@class='list']"));
// Now get all the TR elements from the table but excludes the header row
List<WebElement> allRows = table.findElements(By.xpath(".//tr[not(@class='headerRow')"));
// And iterate over them, getting the cells
for (WebElement row : allRows) {
//assuming that you are actually trying to click on the link for each user name,
//and not just the second element
WebElement cell = row.findElement(By.xpath("./th/a");
// And so on
cell.click();