April.08.2022——Selenium基础(二)
import java.awt.List;
import java.util.*;
import javax.swing.plaf.basic.BasicGraphicsUtils;
import org.apache.bcel.generic.NEW;
import org.apache.commons.io.output.CloseShieldOutputStream;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import com.gargoylesoftware.htmlunit.javascript.background.JavaScriptExecutor;
public class SeleniumAction {
public WebDriver driver;
public void InitDriver() {
System.setProperty("webdriver.chrome.driver","C:\\Program Files\\Google\\Chrome\\chromedriver.exe");
driver = new ChromeDriver();
driver.get("http://www.imooc.com");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void InputElement() {
WebElement EmailElement= driver.findElement(By.name("email"));
EmailElement.sendKeys("你自己的电话号码");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
EmailElement.clear();
String UserInfo = EmailElement.getAttribute("placeholder");
System.out.println(UserInfo);
String moblie = EmailElement.getAttribute("value");
System.out.println(moblie);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
boolean Info = EmailElement.isEnabled();
System.out.println(Info);
driver.close();
}
public void Radio() {
driver.get("https://www.imooc.com/user/setprofile");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
driver.findElement(By.className("js-edit-info")).click();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
List SexList = (List) driver.findElements(By.name("sex"));
((java.util.List<WebElement>) SexList).get(1).click();
driver.close();
WebElement UseForm = driver.findElement(By.id("profile"));
SexList = (List) UseForm.findElement(By.name("sex"));
((java.util.List<WebElement>) SexList).get(1).click();
driver.close();
}
public void CheckBox() {
WebElement Box = driver.findElement(By.id("auto-signin"));
System.out.println(Box.isSelected());
System.out.println(Box.isEnabled());
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Box.click();
driver.close();
}
public void Button() {
System.out.println("------------隐藏前的定位结果--");
WebElement buttonElement= driver.findElement(By.className("moco-btn-red"));
System.out.println(buttonElement.isDisplayed());
System.out.println(buttonElement.isEnabled());
System.out.println("-----------------隐藏操作------");
String js1 = "document.getElementByClassName(\'classname\')[0].style.display = '\none\';";
JavaScriptExecutor js = (JavaScriptExecutor)driver;
js.executeScript(js1);
System.out.println("--------隐藏后的定位结果-----");
WebElement buttonElement2=driver.findElement(By.className("moco-btn-red"));
System.out.println(buttonElement2.isDisplayed());
System.out.println(buttonElement2.isEnabled());
}
public static void main(String[] args) {
SeleniumAction seleniumAction= new SeleniumAction();
seleniumAction.InitDriver();
seleniumAction.InputElement();
seleniumAction.Radio();
seleniumAction.Button();
seleniumAction.CheckBox();
}
}
|