How to type in textbox using Selenium WebDriver (Selenium 2) with Java?

后端 未结 6 2122
栀梦
栀梦 2021-01-04 10:18

\"enter I am using Selenium 2. But after running following code, i could not

相关标签:
6条回答
  • 2021-01-04 10:23

    Thanks Friend, i got an answer. This is only possible because of your help. you all give me a ray of hope towards resolving this problem.

    Here is the code:

    package facebook;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.interactions.Actions;
    
    public class Facebook {
        public static void main(String args[]){
            WebDriver driver = new FirefoxDriver();
            driver.get("http://www.facebook.com");
            WebElement email= driver.findElement(By.id("email"));
            Actions builder = new Actions(driver);
            Actions seriesOfActions = builder.moveToElement(email).click().sendKeys(email, "gati.naveen@gmail.com");
            seriesOfActions.perform();
            WebElement pass = driver.findElement(By.id("pass"));
            WebElement login =driver.findElement(By.id("u_0_b"));
            Actions seriesOfAction = builder.moveToElement(pass).click().sendKeys(pass, "naveench").click(login);
            seriesOfAction.perform();
            driver.
        }    
    }
    
    0 讨论(0)
  • 2021-01-04 10:28

    This is simple if you only use Selenium WebDriver, and forget the usage of Selenium-RC. I'd go like this.

    WebDriver driver = new FirefoxDriver();
    WebElement email = driver.findElement(By.id("email"));
    email.sendKeys("your@email.here");
    

    The reason for NullPointerException however is that your variable driver has never been started, you start FirefoxDriver in a variable wb thas is never being used.

    0 讨论(0)
  • 2021-01-04 10:32

    Another way to solve this using xpath

    WebDriver driver =  new FirefoxDriver();
    driver.get("https://www.facebook.com/");
    driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
    driver.findElement(By.xpath(//*[@id='email'])).sendKeys("your@email.here");
    

    Hope that will help. :)

    0 讨论(0)
  • 2021-01-04 10:33

    You should replace WebDriver wb = new FirefoxDriver(); with driver = new FirefoxDriver(); in your @Before Annotation.

    As you are accessing driver object with null or you can make wb reference variable as global variable.

    0 讨论(0)
  • 2021-01-04 10:33

    Try this :

        driver.findElement(By.id("email")).clear(); 
    driver.findElement(By.id("email")).sendKeys("emal@gmail.com");
    
    0 讨论(0)
  • 2021-01-04 10:35

    You can use JavaScript as well, in case the textfield is dithered.

    WebDriver driver=new FirefoxDriver();
    driver.get("http://localhost/login.do");
    driver.manage().window().maximize();
    RemoteWebDriver r=(RemoteWebDriver) driver;
    String s1="document.getElementById('username').value='admin'";
    r.executeScript(s1);
    
    0 讨论(0)
提交回复
热议问题