【问题标题】:How to make click on the Update Button in the shopping Cart Page - Url="http://live.guru99.com/"如何在购物车页面点击更新按钮 - Url="http://live.guru99.com/"
【发布时间】:2016-08-26 09:08:28
【问题描述】:

尽管 x-path 选择了元素,并且我尝试使用 java executor,但是在代码运行时,它只能单击购物车中第一个元素的更新按钮。以下是代码:

在将 3 个产品放入购物车后,我已将文本框添加到列表中:

我已附上图片: 网址="http://live.guru99.com/"

List<WebElement> li2 =driver.findElements(By.xpath(".//td[@class='product- 
cart-actions']/input"));


for(int j=0;j<li2.size();j++)
    {

        if(j==0)
        {
            li2.get(j).clear();
        li2.get(j).sendKeys("4");
        driver.findElement(By.xpath(".//td[@class='product-cart-actions']/button[@title='Update']")).click();
        li2 =driver.findElements(By.xpath(".//td[@class='product-cart-actions']/input"));
        }
        else if(j==1)
        {
            li2.get(j).clear();
            li2.get(j).sendKeys("2");
            //wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//td[@class='product-cart-actions']/button[@title='Update' and @type='submit']")));
            //driver.findElement(By.xpath(".//td[@class='product-cart-actions']/button[@title='Update' and @type='submit']")).isDisplayed();

            WebElement element = driver.findElement(By.xpath(".//td[@class='product-cart-actions']/button[@title='Update' and @type='submit']"));
            try {
                if (element.isEnabled() && element.isDisplayed()) {
                    System.out.println("Clicking on element with using java script click");

                    ((JavascriptExecutor) driver).executeScript("arguments[0].click();", element);
                } else {
                    System.out.println("Unable to click on element");
                }
            } catch (StaleElementReferenceException e) {
                System.out.println("Element is not attached to the page document "+ e.getStackTrace());
            } catch (NoSuchElementException e) {
                System.out.println("Element was not found in DOM "+ e.getStackTrace());
            } catch (Exception e) {
                System.out.println("Unable to click on element "+ e.getStackTrace());
            }
        //  if (element.isDisplayed()) {
            //  element.click();
            //}


            li2 =driver.findElements(By.xpath(".//td[@class='product-cart-actions']/input"));
        }


        else
        {
            li2.get(j).clear();
            li2.get(j).sendKeys("3");

            WebElement element1 = driver.findElement(By.xpath(".//td[@class='product-cart-actions']/button[@title='Update']"));

        if (element1.isDisplayed()) {
             element1.click();
            }       
        }

【问题讨论】:

  • @guy:你能看看这个吗?

标签: java selenium selenium-webdriver automation selenium-chromedriver


【解决方案1】:

这是罪魁祸首:-

WebElement element = driver.findElement(By.xpath(".//td[@class='product-cart-actions']/button[@title='Update' and @type='submit']"));

因此,当您有多个具有相同定位器的元素时,WebDriver 点击第一个并继续前进。因此,如果您想单击 2nd3rd 元素,您可以修改您的 xpath,例如:-

.//td[@class='product-cart-actions']/button[@title='Update' and @type='submit'][2]   -- For 2nd Element

.//td[@class='product-cart-actions']/button[@title='Update' and @type='submit'][3]  -- For 3rd element.

但这不是一个好的方法,将来可能会有多个,你不会到处为每个元素添加索引。

最好的方法是参考product name(在您的情况下为unique)来识别update按钮。

【讨论】:

  • 它不工作,我试过了。添加 [2] 和 [3] 不是选择产品
  • //tr[2]//td[@class='product-cart-actions']/button[@title='Update' and @type='submit'] 这行得通:chaged tr
  • 我刚刚给出了一个粗略的想法。我不检查 html 很高兴它为你工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-18
  • 2023-03-10
  • 1970-01-01
  • 2021-12-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多