java
  • if-statement
  • selenium-webdriver
  • 2017-06-16 78 views 0 likes 
    0

    我正在寫一個自動化測試一個網頁,其中用戶基於其存在硒網絡驅動程序的if-else條件兩個按鈕

    我使用硒的webdriver有一個按鈕點擊之間進行選擇爪哇 方案 1.只有一個按鈕可以存在於時間(button1button2) 2.如果button1存在時,在該 3.如果button2用戶點擊存在時,在該

    代碼用戶的點擊次數:

    if(driver.findElement(By.xpath("//*[@id='product_container_1']/div[2]/div[2]/div[3]/div[2]/div/a/div")).isDisplayed()) { 
         //clicking on button1 if its presemt 
    
         WebElement clickBtn1 = driver.findElement(By.xpath("//*[@id='product_container_1']/div[2]/div[2]/div[3]/div[2]/div/a/div")); 
         clickBtn1.click(); 
    
    } 
        else if(driver.findElement(By.xpath("//*[@id='product_container_1']/div[2]/div[2]/div[3]/div[3]/div[2]/div")).isDisplayed()){ 
        //Clicking on button2 if its present 
    
        WebElement clickBtn2 = driver.findElement(By.xpath("//*[@id='product_container_1']/div[2]/div[2]/div[3]/div[3]/div[2]/div")); 
        clickBtn2.click(); 
    } 
    
    else 
        { 
         System.out.println("No such button found"); 
        } 
    

    上面的代碼片段不起作用。有人可以幫助糾正它。

    注:如果我刪除的if-else條件和運行分別爲每個按鈕的功能它的工作原理

    感謝,

    +0

    什麼不起作用?它是否說「沒有找到這樣的按鈕」? – nCessity

    +0

    是的。它會拋出錯誤「org.openqa.selenium.NoSuchElementException:no such element:」如果我爲兩個按鈕單獨運行代碼而沒有其他條件,它會起作用 – Harshini

    +0

    這些xpath表達式非常長。你能證實他們工作嗎?你可以通過添加一個id到按鈕來簡化它們嗎? – nCessity

    回答

    0

    謝謝大家誰幫助。如果任何人將來需要這個代碼,我在這裏發佈工作代碼。

    場景: - 網頁上有兩個按鈕 - button1和button2。 - button1將首先出現 - 如果用戶最初單擊button1,第二次button2將會出現。 (在這種情況下,Button1的是從UI隱藏)

    代碼塊:

    if(!driver.findElements(By.xpath("//*[@id='product_container_1']/div[2]/div[2]/div[3]/div[3]/div[2]/div")).isEmpty()){ 
    
    //Clicking on button2, which appears when button1 is not present 
           WebDriverWait wait = new WebDriverWait(driver, 30); 
           wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id='product_container_1']/div[2]/div[2]/div[3]/div[3]/div[2]/div"))); 
    
           Thread.sleep(5000); 
           driver.findElement(By.xpath("//*[@id='product_container_1']/div[2]/div[2]/div[3]/div[3]/div[2]/div")).click(); 
    
         } 
    
         else if(driver.findElement(By.xpath("//*[@id='product_container_1']/div[2]/div[2]/div[3]/div[2]/div/a/div")).isEnabled()){ 
    
    //clicking on button1 
           WebDriverWait wait = new WebDriverWait(driver, 30); 
           wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='product_container_1']/div[2]/div[2]/div[3]/div[2]/div/a/div"))); 
    
         driver.findElement(By.xpath("//*[@id='product_container_1']/div[2]/div[2]/div[3]/div[2]/div/a/div")).click(); 
    
    
        } 
        else 
         { 
          System.out.println("No such button found"); 
         } 
         Thread.sleep(5000); 
    
    0

    您的if-else邏輯之前嘗試使用明確的等待。

    WebDriverWait wait = new WebDriverWait(driver, 30); 
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='product_container_1']/div[2]/div[2]/div[3]/div[2]/div/a/div"))); 
    

    讓我知道這是否有幫助。

    +0

    對不起,這也沒有工作。它拋出相同的錯誤元素未找到 – Harshini

    +0

    但這段代碼工作 - if(!driver.findElements(By.xpath(「xpath」))。isEmpty()) – Harshini

    相關問題