2016-12-29 37 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()不工作

相关问题