简体   繁体   中英

Cannot find javascript frame with webdriver

so I have never had an issue like this before so this goes a bit over my head. I am trying to write a java program that checks store availability in a site like staples. http://www.staples.com/Kodak-EasyShare-Z5010-Digital-Camera/product_369838 After I go to a site like that and click "Check in Store Availability", a javascript window pops up. Using firebug and firepath, I found the zipcode input box has an xpath of ".//*[@id='zipCode']". When I try to enter information into that box in my program, webdriver cannot find the text box. An additional note is that I cannot actually find the box with firebug either unless I click it first.

My question is: how do I navigate to that box with webdriver? I am using selenium 2.21 for this. In case anyone feels inclined to try running my code, I broke the key part out into a runnable program

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM