简体   繁体   English

当我在同一页面上有15个以上具有不同xpath的按钮时,无法继续下一个按钮

[英]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.

相关问题 单击按钮移动到下一页时出错 - Error when i click the button for move to the next page 按钮的Xpath,文本为“新” - Xpath for button having text as 'New' 当多个按钮具有相同的XPATH时,无法单击单个按钮 - Not able to click on single button when there are multiple buttons with same XPATH 硒-每个页面都有一个“下一个”按钮,但相同的按钮并不总是指向相同的页面 - Selenium - Every page has a 'next' button but the same button doesn't always lead to the same page 无法使用Selenium WebDriver单击继续付款按钮 - Unable to click on proceed to payment button using selenium webdriver 我想写在文本框中,如果它显示在页面上,如果它没有显示在页面上,则单击下一步按钮,然后直接单击下一步按钮 - i want to write in textbox if it is display on page and click on next button if it is not display on the page then directly click on next button 我对每个单选按钮都有相同的答案 - I have the same answers on each Radio Button 在我的android项目中,有两个类,并且在第一类上有一个按钮,当我单击按钮时,我需要查看下一页。 - In my android project there are two classes and there is a button on first class,i need to view next page when i click on the button. Selenium Webdriver中的XPath无法单击按钮 - Unable click button by xpath in selenium webdriver 无法使用xpath或ID或名称单击按钮 - Unable to click a button using xpath or id or name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM