0

enter image description here xパスが要素を選択しても、私はjava executorで試しましたが、コード実行時には、カート内の最初の要素の更新ボタンのみをクリックすることができます。私は、画像を添付したショッピングカートのページの更新ボタンをクリックする方法 - Url = "http://live.guru99.com/"

: のURL =は "http://live.guru99かつて私はリストにあったテキストボックスを追加したカート内の3つの製品を設置した後、ここで

:以下のコードです。 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(); 
      }  
     } 
+0

@guy:これを見てもらえますか? –

答えて

1

これが犯人である: - だから、

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

あなたが同じロケータを持つ複数の要素を持って、最初のANにWebDriverクリックdが先に進む。あなたが2nd3rd要素をクリックしたいのであれば、あなたは次のようにあなたのXPath何かを変更することができます -

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

OR

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

しかし、それは将来的に複数があるかもしれない素敵なアプローチではありませんそれぞれの要素ごとにインデックスを追加することはありません。

あなたのケースではuniqueproduct nameを参照して、updateボタンを識別することをお勧めします。

+0

それは働いていない、私は同じを試みた。 [2]と[3]を追加しても、製品を選択していない –

+0

// tr [2] // td [@ class = '製品カートアクション]/button [@ title ='更新 'と@ ]これは動作します:trをchagedしました –

+0

私はちょうど大まかな考えを与えました。私は 'html'があなたのために働いていることを確認していません。 – Paras

関連する問題