2016-12-29 35 views
0

我寫了一個簡單的代碼來刷新瀏覽器遊戲網頁,並使用selenium網頁瀏覽器自動執行該頁面上的任務。如何檢索Java中的Selenium WebElement的最小值?

我做了一個小圈:

while (true) { 
     driver.findElement(By.linkText("Premium Exchange")); 
     if (driver.findElement(By.id("premium_exchange_stock_wood") //<-- if the value of the wood stock is 64 or higher){ 
      driver.findElement(By.name("buy_wood")).clear(); 
      driver.findElement(By.name("buy_wood")).sendKeys("64"); //enter 64 in the 'buy box' 
      Thread.sleep(1000); 
      driver.findElement(By.xpath("//input[@value='Calculate best offer ']")).click(); //click calculate best offer 
      Thread.sleep(1000); 
      driver.findElement(By.xpath("//div[@id='premium_exchange']/div/div[2]/button")).click(); //click buy 
      Thread.sleep(10000); 


     } 

的頁面看起來是這樣的: Link

通常情況下,「木」的股票爲0。當此股票至少達到64,我想我的代碼執行if語句,並無限循環。我被困在讓程序看到作爲int webelement的價值..我的意圖是這樣的:

如果wood_stock等於64或高於64,購買木材,重複。

任何幫助,將不勝感激!我對硒很新鮮!

回答

0

使用.getText()和parseInt函數功能

String strVal=driver.findElement(By.id("premium_exchange_stock_wood").getText();//gets the value as string 
int intVal=Integer.parseInt(strVal);converts string to integer 
if (intVal>=64){ 
    //Perform further actions 
} 

注意:您可以使用.getAttribute( 「的innerHTML」)或.getAttribute( 「outerHTML」)如果.getText()不工作

相關問題