2013-12-09 163 views
4

我一直在嘗試創建一個小程序來將物品放入購物車。它應該轉到物品所在的頁面並將其添加到購物車。然後,所有賬單信息將通過使用不同java類中的數據來輸入。我每次運行此代碼:Selenium Web Driver無法找到元素

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.util.List; 

import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.firefox.FirefoxDriver; 
import org.openqa.selenium.support.ui.ExpectedConditions; 
import org.openqa.selenium.support.ui.Select; 
import org.openqa.selenium.support.ui.WebDriverWait; 



public class Supreme { 

    public static void main(String[] args) throws Exception{ 
     long start = System.nanoTime(); 
     WebDriver driver = new FirefoxDriver(); 

     BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); 
     driver.get("http://www.supremenewyork.com/shop/hats/selassie-beanie/grey"); 
     WebElement add = driver.findElement(By.name("commit")); 
     add.click(); 

     driver.get("https://www.supremenewyork.com/checkout"); 

     AccountInfo a = new AccountInfo(); 
     a = a.getAccount(); 

     WebElement name = driver.findElement(By.id("order_billing_name")); 
     name.sendKeys(a.getName()); 

     WebElement email = driver.findElement(By.id("order_email")); 
     email.sendKeys(a.getEmail()); 

     WebElement phone = driver.findElement(By.id("order_tel")); 
     phone.sendKeys(a.getPhone()); 

     WebElement address1 = driver.findElement(By.id("order_billing_address")); 
     address1.sendKeys(a.getAddress1()); 

     WebElement address2 = driver.findElement(By.id("order_billing_address_2")); 
     address2.sendKeys(a.getAddress2()); 

     WebElement city = driver.findElement(By.id("order_billing_city")); 
     city.sendKeys(a.getCity()); 

     WebElement zip = driver.findElement(By.id("order_billing_zip")); 
     zip.sendKeys(a.getZip()); 

     Select state = new Select(driver.findElement(By.id("order_billing_state"))); 
     state.selectByVisibleText(a.getState()); 

     Select type = new Select(driver.findElement(By.id("credit_card_type"))); 
     type.selectByVisibleText(a.getType()); 

     WebElement credit = driver.findElement(By.id("credit_card_number")); 
     credit.sendKeys(a.getCredit()); 

     Select creditmonth = new Select(driver.findElement(By.id("credit_card_month"))); 
     creditmonth.selectByVisibleText(a.getExpMonth()); 

     Select credityear = new Select(driver.findElement(By.id("credit_card_year"))); 
     credityear.selectByVisibleText(a.getExpYear()); 

     WebElement cvv = driver.findElement(By.id("credit_card_verification_value")); 
     cvv.sendKeys(a.getCVV()); 

     List<WebElement> check = driver.findElements(By.className("iCheck-helper")); 
     for(WebElement w : check){ 
      w.click(); 
     } 

     WebElement process = driver.findElement(By.name("commit")); 
     process.click(); 
    } 
} 

我得到這個錯誤:

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"id","selector":"order_billing_name"} 

感謝您的幫助!

+0

請導入線程,看看它是否有效? –

回答

6

看起來像時間問題給我。在重定向到結帳之後,您可能希望在交互之前等待這些元素。請參閱文檔中的Explicit Waits

WebDriverWait wait = new WebDriverWait(driver, 60);// 1 minute 
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("order_billing_name"))); 
driver.findElement(By.id("order_billing_name")).sendKeys(a.getName()); 
+0

可以使用隱式或顯式等待。或者我們可以使用線程。睡眠(毫秒)http://docs.seleniumhq.org/docs/04_webdriver_advanced.jsp – Tester

+0

對我很好! – Michael

3

你必須檢查是在和IFRAME這個元素如果是則先切換到的iframe,其次如果ID沒有再工作使用XPath或CSS路徑可以請您與我分享HTML源代碼。