[英]Unable to proceed to next button when I have 15+ button on same page having different xpath
Unable to proceed to next button when I have 15+ button on same page having different xpath 当我在同一页面上有15个以上具有不同xpath的按钮时,无法继续下一个按钮
List<WebElement> alllinks = driver.findElements(By.xpath("//a[text()='Edit']"));
// To print the total number of links
String a[] = new String[alllinks.size()];
try
{
for (int i = 0; i < alllinks.size(); i++)
{
a[i] = alllinks.get(i).getText();
if (a[i].startsWith("E"))
{
System.out.println("clicking on this link::" + driver.findElement(By.linkText(a[i])).getText());
driver.findElement(By.linkText(a[i])).click();
driver.findElement(By.xpath("//button[@name='save']")).click();
} else
{
System.out.println("does not starts with E so not clicking");
}
}
} catch (StaleElementReferenceException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
When I click on Edit button then it will get click successfully and working for Save button too. 当我单击“编辑”按钮时,它将成功单击并也可以用于“保存”按钮。 But while click on another(next) EDIT button it fails to click on second EDIT button.
但是,当单击另一个(下一个)编辑按钮时,它无法单击第二个编辑按钮。
Check the attached image having EDIT and Save button. 检查附带的图像具有“编辑”和“保存”按钮。
Please try this. 请尝试这个。 It gets all column and clicks one by one in for each loop.
它获取所有列,并在每个循环中一一单击。 No matter how many edit buttons are there.
无论那里有多少个编辑按钮。 After edit, it will click save each time.
编辑后,每次都会单击保存。 You can modify as per your requirement.
您可以根据需要进行修改。
List<WebElement> alllinks = driver.findElements(By.xpath("//div[@id='customers-grid']/table/tbody/tr/td")); // here give unique id if this one is not unique.
try
{
for (WebElement ele : alllinks )
{
ele.click();
driver.findElement(By.xpath("//button[@name='save']")).click();
}
} catch (StaleElementReferenceException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
The problem is that you start by getting all the Edit links on the page, you loop through them, but in the middle of the loop you refetch the Edit link (using By.linkText(a[i])
) and only ever get the first one and click on it. 问题是,您首先获取页面上的所有Edit链接,然后循环浏览它们,但是在循环的中间,您重新获取Edit链接(使用
By.linkText(a[i])
),并且仅获得第一个,然后单击它。
You already fetched all the Edit links, you don't need to refetch anything (and you don't need to verify that the link starts with "E" since that's part of your locator, text()='Edit'
. 您已经获取了所有的Edit链接,不需要重新获取任何内容(也不需要验证链接以“ E”开头,因为这是定位符
text()='Edit'
。
Also, you can avoid the StaleElementException
by refetching the elements on each loop. 另外,您可以通过在每个循环上重新获取元素来避免
StaleElementException
。 StaleElementException
s happen because the page (or a part of the page) reloads and you try to use a variable storing an element reference from before the reload. 发生
StaleElementException
是因为重新加载页面(或页面的一部分),并且您尝试使用变量来存储重新加载之前的元素引用。
Simplified code is below. 简化的代码如下。
By editButtonLocator = By.xpath("//a[text()='Edit']");
List<WebElement> alllinks = driver.findElements(editButtonLocator);
for (int i = 0; i < alllinks.size(); i++)
{
alllinks.get(i).click();
driver.findElement(By.xpath("//button[@name='save']")).click();
// get the list again to avoid StaleElementException
alllinks = driver.findElements(editButtonLocator);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.