2017-07-17 76 views
-2

如何,如果條件如何使用,如果在硒網絡的駕駛員條件

driver.findelement(By.xpath("id").sendkey(""); 

在這裏使用例如

.xplath(id=253).click else xpath(id=888).sendkey("admin"); 
+0

如果'id'是253你想使用'click',並且如果'id'是888你想輸入什麼? – Guy

+0

https://stackoverflow.com/a/45138343/8305604感謝它爲我工作:D :) – sanjan

回答

1

嘗試這種方式,首先找到webelement 253,如果ID 253不獲得查找然後,您的代碼執行跳轉到catch塊。

try 
    { 
     WebElement element = driver.findElement(By.id("253")); 

     if(element.isDisplayed() && element.isEnabled()) 
     { 
      element.click(); 
     } 
    } 
    catch(Exception e) 
    { 
     WebElement element_1 = driver.findElement(By.id("888")); 

     if(element_1.isDisplayed() && element_1.isEnabled()) 
     { 
      element_1.sendKeys("admin"); 
     } 
    } 
+0

嘿我想在下拉列表中使用此代碼如果用戶點擊前面例如我有2個選項在該列表中,如果用戶selet 1st好的,如果用戶選擇第二個我如何編碼?這些是我的代碼,並希望使用別的也 WebElement元素= driver.findElement(By.id(「.//* [@ id ='service_autoinput_dropdown']/div [6]/div [1]」「)) ; WebElement element_1 = driver.findElement(By.id(「.//* [@ id ='service_autoinput_dropdown']/div [7]/div [2]」「)); 謝謝 – GHOST

+0

thz其工作plz幫助我到我的另一個問題 – GHOST

+0

等待90分鐘我有很多問題 – GHOST

3

首先檢查哪個元素在那裏。無論是id = 253還是id = 888.我們可以簡單地在java中使用findelements來實現這一點。

if(driver.findElements(by.xpath("//*[@id=253]")).size>0) 
{ 
    //element exists with id = 253 
    // do the stuff 
} else 
{ 
    //element do not exist with id = 253. 
    //element with id - 888 exists 
    // do the stuff 
} 

希望這會有所幫助。謝謝。