Selenium Web自动化,获取特定的HTML元素

时间:2017-11-09 04:03:53

标签: java html selenium xpath web-scraping

我一直在巡视我的项目,但遇到了一个我无法找到任何具体信息的问题。我想抓住并保存密码。

HTML:

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

保存:cai3Yeej

代码:

代码问题在获取信息方法的底部:

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;

        }

    }

}

HTML的其余部分:

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

如果您有任何想法或建议,请告诉我们!

谢谢,Ben。

1 个答案:

答案 0 :(得分:1)

请尝试以下代码:

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

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