jeudi 14 décembre 2017

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.

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://ift.tt/U540Ox");
        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 = "eclipse@gmail.com";
    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.

Aucun commentaire:

Enregistrer un commentaire