Java Selenium web自动化,获取特定的HTML元素
我一直在浏览我的副业项目,但遇到了一个我找不到任何具体信息的问题。我想抓取并保存密码 HTML: 保存:cai3yej 代码: 代码问题位于获取信息方法的底部:Java Selenium web自动化,获取特定的HTML元素,java,html,selenium,xpath,web-scraping,Java,Html,Selenium,Xpath,Web Scraping,我一直在浏览我的副业项目,但遇到了一个我找不到任何具体信息的问题。我想抓取并保存密码 HTML: 保存:cai3yej 代码: 代码问题位于获取信息方法的底部: public class getter { private String username, password, contactInfo; private int phoneNumber, phoneCarrier; Scanner s = new Scanner(System.in); public
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;
}
}
}
公共类getter{
私有字符串用户名、密码、联系人信息;
私人int电话号码,电话运营商;
扫描仪s=新的扫描仪(System.in);
公共getter(){
System.out.println(“您好,欢迎来到来宾网络\n”);
System.out.println(“请输入您的用户名:”);
username=s.nextLine();
System.out.println(“请输入您的密码:”);
密码=s.nextLine();
System.out.println(“请输入您的电话号码:”);
phoneNumber=s.nextInt();
System.out.println(“请输入您的电话运营商的相应号码”);
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(“谢谢!当您的网络密码更新时,您将收到一条文本”);
电话();
getInfo();
}
//网络自动化
public void getInfo(){
//Chrome本地主机驱动程序
System.setProperty(“webdriver.chrome.driver”,“C:\\SeleniumDrivers\\chromedriver.exe”);
//创建一个新的webdirver并打开chrome
WebDriver驱动程序=新的ChromeDriver();
//驱动程序获得链接
get(“出于隐私,链接必须删除”);
//驱动程序查找元素并输入用户名和密码
driver.findElement(按名称(“用户名”)).sendKeys(用户名);
driver.findElement(按名称(“密码”)).sendKeys(密码);
driver.findElement(按.name(“提交”))。单击();
//等待页面加载
WebDriverWait wait=新的WebDriverWait(驱动程序,10);
wait.until(ExpectedConditions.elementtobelickable(By.id(“addguest”));
//第2页
driver.findElement(By.id(“添加来宾”))。单击();
//将名称添加到名称/说明中
driver.findElement(By.name(“comment”)).sendKeys(用户名);
//添加用户名的第二部分
driver.findElement(By.name(“用户名”)).sendKeys(“WIFIAuto”);
//添加用户的电子邮件地址
驱动程序.findElement(通过名称(“电子邮件地址”)).sendKeys(联系信息);
driver.findElement(By.id(“网络”))。单击();
//提交信息
WebElement=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']);
动作动作=新动作(驱动);
action.moveToElement(元素)。单击()。执行();
//第三页
wait.until(ExpectedConditions.elementToBeClickable(By.xpath(“//tbody/tr/td[@class='sorting_1']”));
findElement(By.xpath(“//tbody/tr/td[@class='sorting_1']”)。单击();
//第四页
//获取要发送到存在我的问题的电子邮件/文本的密码
列表密码=
driver.findElements(By.xpath(“//div/p/code[@class='password']);
System.out.println(密码);
}
//向用户发送带有WIFI用户名和密码的电子邮件
公开作废电子邮件(){
}
//发送带有WIFI用户名和密码的用户文本
公用电话{
交换机(电话载波){
案例1:contactInfo=phoneNumber+“@tmomail.net”;
打破
案例2:contactInfo=phoneNumber+“@vmobl.com”;
打破
案例3:contactInfo=phoneNumber+“@cingularme.com”;
打破
案例4:contactInfo=phoneNumber+“@messaging.sprintpcs.com”;
打破
案例5:contactInfo=phoneNumber+“@vtext.com”;
打破
默认值:contactInfo=phoneNumber+“@messaging.nextel.com”;
打破
}
}
}
HTML的其余部分:
<div class="section">
<p>
Password:
<code class="password">cai3Yeej</code>
密码:
cai3Yeej
如果你有任何想法或建议,请让我知道
谢谢,本。请尝试以下代码:
password = driver.findElement(By.cssSelector(".section>p>code")).getAttribute("value");
或
你能解释清楚到底是什么问题吗?太棒了!感谢you@Ben,如果我的答案解决了您的问题,请勾选(我答案左侧的)勾号。谢谢。是的,再次谢谢。对不起,打勾检查太晚了
password = driver.findElement(By.cssSelector(".section>p>code")).getAttribute("value");
password = driver.findElement(By.cssSelector(".section>p>code")).getText();