我正在寫一個自動化測試一個網頁,其中用戶基於其存在硒網絡驅動程序的if-else條件兩個按鈕
我使用硒的webdriver有一個按鈕點擊之間進行選擇爪哇 方案 1.只有一個按鈕可以存在於時間(button1
或button2
) 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條件和運行分別爲每個按鈕的功能它的工作原理
感謝,
什麼不起作用?它是否說「沒有找到這樣的按鈕」? – nCessity
是的。它會拋出錯誤「org.openqa.selenium.NoSuchElementException:no such element:」如果我爲兩個按鈕單獨運行代碼而沒有其他條件,它會起作用 – Harshini
這些xpath表達式非常長。你能證實他們工作嗎?你可以通過添加一個id到按鈕來簡化它們嗎? – nCessity