2012-05-25 41 views
0

所以我從來沒有像這樣的問題,所以這有點過頭了。我正試圖編寫一個Java程序來檢查網站上的商店可用性,如主食。 http://www.staples.com/Kodak-EasyShare-Z5010-Digital-Camera/product_369838 當我去到這樣的網站並點擊「Check in Store Availability」後,會彈出一個javascript窗口。使用firebug和firepath,我發現郵政編碼輸入框的xpath是「.//*[@id='zipCode']」。當我嘗試在我的程序中輸入信息時,webdriver找不到文本框。另外需要注意的是,除非我先點擊它,否則我實際上找不到裝有螢火蟲的盒子。找不到與webdriver的javascript框架

我的問題是:如何使用webdriver導航到該框?我爲此使用硒2.21。如果有人感覺傾向於嘗試運行我的代碼,我打破了關鍵部分伸到一個可運行的程序

import java.util.ArrayList; 
import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.firefox.FirefoxDriver; 
import org.openqa.selenium.firefox.FirefoxProfile; 

public class Test{ 

public static void main(String[] args) throws InterruptedException{ 
    ArrayList<String> pIDs = new ArrayList<String>(); 
    FirefoxProfile profile = new FirefoxProfile(); 
    profile.setPreference("general.useragent.override", "some UA string"); 
    WebDriver driver = new FirefoxDriver(profile); 
    String link, availStr; 
    String output = null; 

    //Gets me through their initial zipcode prompt before I can see any products on site 
    //--------------------------------------- 
    String url = "http://www.staples.com/Tablets-Tablets/cat_CL165566"; 
    driver.get(url); 
    driver.findElement(By.xpath(".//*[@id='zip']")).sendKeys("55555"); 
    driver.findElement(By.xpath(".//*[@id='submitLink']")).click(); 
    try { 
     Thread.sleep(1000); 
    } catch (InterruptedException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } 
    //-------------------------------------------- 

    driver.get("http://www.staples.com/Kodak-EasyShare-Z5010-Digital-Camera/product_369838"); 
    System.out.println("Now on item: " + driver.getTitle() + " " + driver.findElement(By.xpath(".//*[@class='note none']")).getText() + "\n"); 
    driver.findElement(By.xpath("//li[@class='storeavail']/a")).click(); 
    Thread.sleep(400); 

    driver.findElement(By.xpath(".//*[@id='zipCode']")).sendKeys("90210"); 
    driver.findElement(By.xpath(".//*[@id='searchdistance']/div/a")).click(); 

    driver.quit(); 
} 

}

回答

2

你應該在框架上的任何元素之前的工作切換到框架..

driver.switchTo().frame(weblement/nameofframe) 
+0

有沒有具體的方法讓我找到框架的名稱?對不起,我是使用硒的新手。 – AlbChu

+0

試試這個。 driver.switchTo(driver.findElement(By.xpath(「// iframe [@ id ='overlayframe']))。我沒有測試這個,這是你的問題中的鏈接 –