第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

使用 if 和 else 語句之外的字符串來設置字符串值

使用 if 和 else 語句之外的字符串來設置字符串值

慕的地6264312 2021-09-12 20:22:07
關(guān)于我遇到的問題,我只是有一個足夠簡單的問題。我試圖設置一個字符串值,"0"如果一個 webelement 不存在,否則字符串是 webelement 值(使用getText)。但是,我似乎無法在if 和 else語句之外使用這些值。我該怎么做呢?這是我的代碼String players_in_game = null;public void join_game_user_increases_register() throws Exception {    WebDriverWait wait = new WebDriverWait(Drivers.getDriver(), 10);    wait.until(ExpectedConditions.visibilityOf(countdownLabel));    if (!num_of_players_in_game.isDisplayed()) {        String players_in_game = "0";    } else {        String players_in_game = num_of_players_in_game.getText();    }    System.out.println(players_in_game);    int first_num = Integer.parseInt(players_in_game);
查看完整描述

3 回答

?
開心每一天1111

TA貢獻1836條經(jīng)驗 獲得超13個贊

使用下面的代碼:


WebDriverWait wait = new WebDriverWait(Drivers.getDriver(), 10);

wait.until(ExpectedConditions.visibilityOf(countdownLabel));


String players_in_game = "0";

if(num_of_players_in_game.isDisplayed()){

   players_in_game = num_of_players_in_game.getText();   

}


System.out.println(players_in_game);

int first_num = Integer.parseInt(players_in_game);

或者:


String players_in_game = num_of_players_in_game.isDisplayed() ? num_of_players_in_game.getText() : "0";

或者:


List<WebElements> num_of_players_in_game = driver.findElements(By....);

String players_in_game = num_of_players_in_game.size()==0 ? "0": num_of_players_in_game.get(0).getText();



查看完整回答
反對 回復 2021-09-12
?
夢里花落0921

TA貢獻1772條經(jīng)驗 獲得超6個贊

你可以試試這個:


String players_in_game = null;


public void join_game_user_increases_register() throws Exception {


WebDriverWait wait = new WebDriverWait(Drivers.getDriver(), 10);

wait.until(ExpectedConditions.visibilityOf(countdownLabel));


try {

    if (num_of_players_in_game.isDisplayed()) {

        String players_in_game = num_of_players_in_game.getText();

    }

} catch (Exception e) {

    String players_in_game = "0";

}


System.out.println(players_in_game);

int first_num = Integer.parseInt(players_in_game);


查看完整回答
反對 回復 2021-09-12
?
萬千封印

TA貢獻1891條經(jīng)驗 獲得超3個贊

由于您已經(jīng)在代碼的第一行中將該變量聲明為類成員,因此String只需刪除即可不將該名稱重新聲明為局部變量,而是使用該字段:


if(!num_of_players_in_game.isDisplayed()){

    players_in_game = "0";

} else {

    players_in_game = num_of_players_in_game.getText();

}


查看完整回答
反對 回復 2021-09-12
  • 3 回答
  • 0 關(guān)注
  • 187 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號