java - Selenium web自动化,获取特定的HTML元素

标签 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>

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

谢谢,本。

最佳答案

尝试以下代码:

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

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

关于java - Selenium web自动化,获取特定的HTML元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47193717/

相关文章:

java - 如何在 SWT 中添加处理 PApplet

java - 在 EAR 构建之后运行测试

PHP 和 mySQL 不工作

java - 通过 XSLT 将 Java 对象转换为 JSON

html - 可以使用多个音频标签吗

javascript - 页面加载时显示输入内容

Selenium 看不到 AngularJS 页面元素

c# - 找不到名为 webdriver.json 的文件或 ID 为“WebDriver.FirefoxPreferences”的嵌入式资源

java - testng 测试通过 selenium 网格返回 ie 驱动程序路径错误,但测试适用于 Firefox

java - Spring Boot SpEL ConditionalOnExpression 检查多个属性