所以我以前从未遇到过这样的问题,所以这有点超出了我的理解范围。我正在尝试编写一个java程序来检查像staples这样的网站中的商店可用性。 http://www.staples.com/Kodak-EasyShare-Z5010-Digital-Camera/product_369838 当我访问类似的网站并单击“检查商店可用性”后,会弹出一个 JavaScript 窗口。使用firebug和firepath,我发现邮政编码输入框的xpath为“.//*[@id='zipCode']”。当我尝试在程序中的该框中输入信息时,webdriver 找不到该文本框。附加说明是,除非我先单击它,否则我实际上也找不到带有 Firebug 的框。
我的问题是:如何使用 webdriver 导航到该框?我为此使用selenium 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();
}
}
最佳答案
在处理框架中的任何元素之前,您应该切换到框架。
driver.switchTo().frame(weblement/nameofframe)
关于java - 无法使用 webdriver 找到 javascript 框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10758750/