Selenium Send Keys not Working with java while sub

2019-09-12 04:38发布

问题:

I am not able to perform the send keys operation while submitting a form using the locator method (Xpath). While submitting the forms or while login I am not able to find the element by using the 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();
    }
}

Also how do I find out the frame id as frame(0).......

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

I am trying to fill in this form and I am not able to fill in the details using the selenium commands.

回答1:

All Elements of your page are within iframe so you have to first switch to iframe :

Add following before login :

driver.switchTo().frame(0);

So it will be :

 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();
}

I just checked , it should work now for you.