【问题标题】:Selenium web automation, obtain specific HTML elementsSelenium web 自动化,获取特定的 HTML 元素
【发布时间】:2017-11-09 04:03:52
【问题描述】:

我一直在浏览我的业余项目,但遇到了一个我找不到任何具体信息的问题。我正在寻找并保存密码。

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>

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

谢谢,本。

【问题讨论】:

  • 您能否更清楚地解释一下具体的问题是什么?

标签: java html selenium xpath web-scraping


【解决方案1】:

试试下面的代码:

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

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

【讨论】:

  • 太棒了!谢谢
  • @Ben,如果我的回答解决了您的问题,请勾选(在我的回答左侧)。谢谢。
  • 是的,再次感谢。对不起,迟到的勾号检查
猜你喜欢
  • 2020-06-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-24
  • 2021-11-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多