0

EDIT: Never mind, I figured it out. Had to do By.name() instead of By.id().

I am trying to learn how to use Selenium with XML files but seem to have run into a problem.

Steps:

  • I parse the XML file and save the values into strings
  • Using Selenium WebDriver, I open up Google and try to insert some value in the search box and hit "Google Search".

This is where I get stuck. The value is never inputted and it never clicks the button.

XML File:

<?xml version="1.0" encoding="UTF-8"?>

<TestData>
    <url-name>
        <url>http://www.google.com</url>
    </url-name>

    <user-details>
        <email>[email protected]</email>
        <phone>(555)5559292</phone>
        <folder>inbox</folder>
    </user-details>
</TestData> 

Java Code:

import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.*;
import org.w3c.dom.*;

public class XMLTest 
{
    public static void main(String [] args)
    {
        try {
            // Get xml file
            File file = new File("input.xml");

            // Prepare XML
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document document = db.parse(file);

            String url = document.getElementsByTagName("url").item(0).getTextContent();
            String email = document.getElementsByTagName("email").item(0).getTextContent();
            String phone = document.getElementsByTagName("phone").item(0).getTextContent();
            String folder = document.getElementsByTagName("folder").item(0).getTextContent();

            System.out.println("\n [Debug Info]\n ------------"
                             + "\n Mail:\t\t" + url
                             + "\n Email:\t\t" + email
                             + "\n Phone:\t\t" + phone
                             + "\n Folder:\t" + folder);

            // Selenium code
            WebDriver wd = new FirefoxDriver();

            wd.manage().timeouts().implicitlyWait(60,  TimeUnit.SECONDS);
            wd.get(url);    // Go to URL
            wd.findElement(By.id("q")).sendKeys(phone); // Type into google search box
            wd.findElement(By.id("btnK")).click(); // Click button
            Thread.sleep(2000);

            System.out.println("\n\n [Selenium]\n -----------");
            System.out.println("\n Title:\t\t" + wd.getTitle()
                             + "\n URL:\t\t" + wd.getCurrentUrl());

            wd.close();

        } catch(Exception e) {
            e.printStackTrace();
        }
    }
}

The browser opens but it just stays on the Google homepage. Any help would be appreciated, thank you!

2
  • have you got the solution or not your update is not clear Commented May 17, 2016 at 14:44
  • I did. Abhi just posted it as well. Commented May 17, 2016 at 14:48

1 Answer 1

2

'q' and 'btnK' are element names and not Ids. Try this.

 wd.findElement(By.name("q")).sendKeys(phone); // Type into google search box
 wd.findElement(By.name("btnK")).click(); // Click button
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.