Selenium发送密钥在提交表单时不使用java

时间:2015-08-12 08:15:30

标签: java forms selenium xpath

使用定位器方法(Xpath)提交表单时,我无法执行发送键操作。在提交表单或登录时,我无法使用xpath找到该元素。

package accomplishment_Tracker;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class ects {
    public WebDriver driver;

    @BeforeTest
    public void openurl(){
        //driver =new FirefoxDriver();
        FirefoxProfile firefoxProfile = new FirefoxProfile();
        firefoxProfile.setPreference("reader.parse-on-load.enabled",false);
        driver = new FirefoxDriver(firefoxProfile);
        driver.get("http://66.192.160.50/ects/");
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        System.out.println("Website opened succesfully");
    }

    //LOGIN
    @Test
    public void login(){
        driver.findElement(By.xpath("html/body/div[1]/div/div/div/div[2]/form/fieldset/div[1]/input")).sendKeys("abcde");//("admin");
        driver.findElement(By.xpath("/html/body/div[1]/div/div/div/div[2]/form/fieldset/div[2]/input")).sendKeys("password");
        driver.findElement(By.xpath("/html/body/div[1]/div/div/div/div[2]/form/fieldset/div[3]/label/input")).click();
        driver.findElement(By.xpath("/html/body/div[1]/div/div/div/div[2]/form/fieldset/input")).click();
    }

    @AfterTest
    public void close(){
        driver.quit();
    }
}

另外,我如何找出帧ID(0).......

http://www.ecomnets.com/careers/submit-a-resume/

我正在尝试填写此表单,但我无法使用selenium命令填写详细信息。

1 个答案:

答案 0 :(得分:4)

您网页的所有元素都在iframe中,因此您必须先切换到iframe:

登录前添加以下内容:

driver.switchTo().frame(0);

所以它会是:

 public void login(){

    driver.switchTo().frame(0);
    driver.findElement(By.xpath("html/body/div[1]/div/div/div/div[2]/form/fieldset/div[1]/input")).sendKeys("abcde");//("admin");
    driver.findElement(By.xpath("/html/body/div[1]/div/div/div/div[2]/form/fieldset/div[2]/input")).sendKeys("password");
    driver.findElement(By.xpath("/html/body/div[1]/div/div/div/div[2]/form/fieldset/div[3]/label/input")).click();
    driver.findElement(By.xpath("/html/body/div[1]/div/div/div/div[2]/form/fieldset/input")).click();
}

我刚检查过,它现在应该适合你。