亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Selenium java中isDisplayed()的替代方法

Selenium java中isDisplayed()的替代方法

智慧大石 2022-01-19 09:51:54
public void privateCohortCreation() {    if(webElements.newCohortElm.isDisplayed()) {        SeleniumUtils.click(getDriver(),webElements.createCohortSelectionFromMenu);        webElements.cohortname.sendKeys("private_cohort_test");        SeleniumUtils.click(getDriver(),webElements.createCohortButton);    }    else {        doApply();    }}我希望如果顯示元素然后執行任務 else 調用doApply()方法。但這是一個例外"no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/app-root/div/app-container/app-indv301/app-global-filters/div/ul/li[3]/app-cohort/div/div/app-status/div"} (Session info: chrome=70.0.3538.77)"
查看完整描述

2 回答

?
弒天下

TA貢獻1818條經驗 獲得超8個贊

您可以使用findElements()來檢查該元素是否在網頁上。


findElements()- 如果沒有具有給定定位器的元素,則返回空列表-如果元素不在頁面上,則

findElement()拋出NoSuchElementException


試試下面的代碼:


List<WebElement> elements = driver.findElements(By.locator);

if(!elements.isEmpty()) {

    if(elements.get(0).isDisplayed()) {

       elements.get(0).click();

    }

    else {

       // element not visible 

    }


}else{

  // here mention code if element not present   

}

建議:使用相對 xpath 而不是絕對 xpath。或嘗試使用 CSS 選擇器。


查看完整回答
反對 回復 2022-01-19
?
月關寶盒

TA貢獻1772條經驗 獲得超5個贊

嘗試使用 try catch 而不是 if else。


 try {

            if (webElements.newCohortElm.isDisplayed()) {

                doApply();

            }

        }

        catch (Exception e){

            SeleniumUtils.click(getDriver(), webElements.createCohortSelectionFromMenu);

            webElements.cohortname.sendKeys("private_cohort_test");

            SeleniumUtils.click(getDriver(), webElements.createCohortButton);

        }


查看完整回答
反對 回復 2022-01-19
  • 2 回答
  • 0 關注
  • 238 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號