J91
J91

Reputation: 55

Finding and providing values to web elements using if-else statements in Selenium WebDriver

I am attempting to type specific values into various text-boxes using Selenium WebDriver, using a Try-Block and If-Else statements to pinpoint the specific web-element, and then provide the appropriate value based on that individual element. My issue is that my if-statements seem to be read as "false", because each time the if-else statement is read through, it goes to the else { block and prints "error".

I have already checked the String values of element.getAttribute("id"), and they do equal the values I have given in the if-else statement.

I have done some digging and while I found some if-else statements related to element visibility and the usage of element.isEnabled(), I have not found anything where element.getAttribute() String values are used to find specific web elements and subsequently send values.

Here is my code:

WebDriver driver;

@Test
public void enterInfo() throws Exception {

     System.setProperty("webdriver.gecko.driver","C:\\Users\\Kohout\\Documents\\geckoDriver\\geckodriver.exe" );
     DesiredCapabilities capabilities = DesiredCapabilities.firefox();
        capabilities.setCapability("marionette", true);
        driver = new FirefoxDriver();

        driver.get("http://www.firstcry.com/");
        Thread.sleep(3000L);

        WebElement element = driver.findElement(By.id("Email"));
        typingInfo(element);

        Thread.sleep(2000L);

        element = driver.findElement(By.id("mobileprefix"));
        typingInfo(element);

        Thread.sleep(2000L);


        element = driver.findElement(By.id("Mobile"));
        typingInfo(element);
    }



public void typingInfo(WebElement element) throws AWTException {

    String email = "[email protected]";
    String countryCode = "1";
    String phoneNumber = "555-318-9357";

    try {

        if(element.getAttribute("id") == "Email") {

            System.out.println("Commencing command...");

            element.sendKeys(email);

        } else if (element.getAttribute("id") ==  "mobileprefix") {

            System.out.println("Commencing command...");

            element.sendKeys(countryCode);
        } else if (element.getAttribute("id") == "Mobile") {

            System.out.println("Commencing command...");

            element.sendKeys(phoneNumber);

        } else {

            System.out.println("Error");
        }

    } catch (Throwable t) {

        System.out.println(t.getStackTrace());

    } 

Thank you for your help and suggestions.

Upvotes: 1

Views: 1694

Answers (2)

Indrapal Singh
Indrapal Singh

Reputation: 364

I thinks it's because you are using == operator not works for string comparision properly. Try code modified.

public void typingInfo(WebElement element) throws AWTException {

String email = "[email protected]";
String countryCode = "1";
String phoneNumber = "555-318-9357";

try {

    if("Email".equalsIgnoreCase(element.getAttribute("id")) {

        System.out.println("Commencing command...");

        element.sendKeys(email);

    } else if ("mobileprefix".equalsIgnoreCase(element.getAttribute("id")) {

        System.out.println("Commencing command...");

        element.sendKeys(countryCode);
    } else if ("Mobile"..equalsIgnoreCase(element.getAttribute("id")) {

        System.out.println("Commencing command...");

        element.sendKeys(phoneNumber);

    } else {

        System.out.println("Error");
    }

} catch (Throwable t) {

    System.out.println(t.getStackTrace());

}

Upvotes: 1

Würgspaß
Würgspaß

Reputation: 4820

Try this:

    if("Email".equals(element.getAttribute("id"))) {
        System.out.println("Commencing command...");
        element.sendKeys(email);
    } else if ("mobileprefix".equals(element.getAttribute("id"))) {
        System.out.println("Commencing command...");
        element.sendKeys(countryCode);
    } else if ("Mobile".equals(element.getAttribute("id"))) {
        System.out.println("Commencing command...");
        element.sendKeys(phoneNumber);
    } else {
        System.out.println("Error");
    }
  • Always use equals for string comparison in Java. == checks object equality.
  • Call equals on string literals to avoid possible NullPointerException.

Upvotes: 1

Related Questions