0

I've been cruising through my side project but ran into an issue that I can't find any specific information on. I'm looking to grab and save the password.

HTML:

<code> class="password">cai3Yeej </code>

Saving: cai3Yeej

Code:

The issue with code is at the bottom of getting info method:

public class getter {

    private String username, password, contactInfo;
    private int phoneNumber, phoneCarrier;
    Scanner s = new Scanner(System.in);

    public getter() {

        System.out.println("Hello and welcome to guest network \n");
        System.out.println("Please enter your username:");
        username = s.nextLine();
        System.out.println("Please enter your password:");
        password = s.nextLine();
        System.out.println("Please enter your phone number:");
        phoneNumber = s.nextInt();
        System.out.println("Please enter the corasponding number for your phone carrier");
        System.out.println(" 1 T-Mobile \n 2 Virgin Mobile \n 3 Cingular \n 4 Sprint \n 5 Verizon \n 6 Nextel");
        phoneCarrier = s.nextInt();
        System.out.println("Thank you! You will recive a text when your network password is updated");
        phone();
        getInfo();

    }
    //Web automation
    public void getInfo() {
    //Chrome local host driver
    System.setProperty("webdriver.chrome.driver","C:\\SeleniumDrivers\\chromedriver.exe");
    //Creates a new webdirver and opens chrome
    WebDriver driver = new ChromeDriver();
    //Driver gets the link 
    driver.get("link had to remove for privacy");
    //Driver finds element anf inputs username and password
    driver.findElement(By.name("username")).sendKeys(username);
    driver.findElement(By.name("password")).sendKeys(password);
    driver.findElement(By.name("submit")).click();
    //Waiting untill the page loads
    WebDriverWait wait = new WebDriverWait(driver, 10);
    wait.until(ExpectedConditions.elementToBeClickable(By.id("add-guest")));
    //Page 2
    driver.findElement(By.id("add-guest")).click();
    //Adds name to Name/Description
    driver.findElement(By.name("comment")).sendKeys(username);
    //Adds second part of username
    driver.findElement(By.name("username")).sendKeys("WIFIAuto");
    //Adds the users email address
    driver.findElement(By.name("email_address")).sendKeys(contactInfo);
    driver.findElement(By.id("network-network")).click();
    //Submits the info
    WebElement element = driver.findElement(By.xpath("//div/button[@class='ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only']/span[@class='ui-button-text']"));
    Actions action = new Actions(driver);
    action.moveToElement(element).click().perform();
    //Third Page
    wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//tbody/tr/td[@class=' sorting_1']")));
    driver.findElement(By.xpath("//tbody/tr/td[@class=' sorting_1']")).click();
    //Fourth Page
    //Getting the password to be sent to email/ text where my issue exists
    List<WebElement> password = 
    driver.findElements(By.xpath("//div/p/code[@class='password']"));
    System.out.println(password);

    }  
    //Sends the user an email with the WIFI username and password
    public void email() {

    }
    //Sends the user text with the WIFI username and password
    public void phone() {

        switch (phoneCarrier) {

            case 1: contactInfo = phoneNumber + "@tmomail.net";
                   break;
            case 2: contactInfo = phoneNumber + "@vmobl.com";
                   break;
            case 3: contactInfo = phoneNumber + "@cingularme.com";
                    break;
            case 4: contactInfo = phoneNumber + "@messaging.sprintpcs.com";
                    break;
            case 5: contactInfo = phoneNumber + "@vtext.com";
                    break;
            default: contactInfo = phoneNumber + "@messaging.nextel.com";
                    break;

        }

    }

}

Rest of HTML:

<div class="section">
<p>
Password:
<code class="password">cai3Yeej</code>

If you have any ideas or suggestions please let me know!

Thanks, Ben.

1
  • 1
    Could you explain more clearly what is the exact problem ? Commented Nov 9, 2017 at 5:01

1 Answer 1

1

Try the following code:

password = driver.findElement(By.cssSelector(".section>p>code")).getAttribute("value");

or

password = driver.findElement(By.cssSelector(".section>p>code")).getText();
Sign up to request clarification or add additional context in comments.

3 Comments

Awesome! Thank you
@Ben, if my answer resolved your question, please check the tick (to the left of my answer). Thanks.
Yes it is, thanks again. Sorry for the late tick check

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.