我无法在用户名中使其成为莱特,在。https:/mail.protonmail.comcreatenew?语言=en。. 不知为什么,它找不到或不能点击 "选择用户名",有人能帮我吗?
代码审判;
public class LaunchApplication {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
int number = 1;
String url = "https://protonmail.com";
String password = "passwordpassword123123";
String username = "";
String characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
Random rand = new Random();
int length = 8;
while (number > 0){
//Create an random string for the user name:
// video of how to make string randomicer https://www.youtube.com/watch?v=CZYeTblcOU8 check it out
char[] text = new char[length];
for(int i = 0; i < length; i++){
text[i] = characters.charAt(rand.nextInt(characters.length()));
}
for(int i = 0; i < text.length; i++){
username += text[i];
}
System.out.println(username);
Thread.sleep(2000);
//Program starting: video to make it all work https://www.youtube.com/watch?v=QY0iQpX0LDU
driver.get(url);
System.out.println(driver.getTitle());
Thread.sleep(2000);
driver.findElement(By.linkText("SIGN UP")).click();
Thread.sleep(5000);
driver.findElement(By.className("panel-heading")).click();
Thread.sleep(2000);
driver.findElement(By.id("freePlan")).click();
Thread.sleep(5000);
System.out.println("Here");
Thread.sleep(5000);
System.out.println("pass1");
driver.findElement(By.name("password")).sendKeys(password);
System.out.println("pass2");
driver.findElement(By.name("passwordc")).sendKeys(password);
Thread.sleep(2000);
System.out.println("Username here");
try{
driver.findElement(By.id("username")).click();
}catch (Exception E){
System.out.println("DOnt work1");
}
try{
driver.findElement(By.name("username")).click();
}catch (Exception B){
System.out.println("DOnt work2");
}
try{
driver.findElement(By.id("input")).click();
}catch (Exception A){
System.out.println("DOnt work3");
}
try{
driver.findElement(By.linkText("Choose")).click();
}catch (Exception A){
System.out.println("DOnt work4");
}
try{
driver.findElement(By.xpath("//input[@id='#username.input']")).click();
}catch (Exception A){
System.out.println("DOnt work5");
}
try{
driver.findElement(By.tagName("messages=\"[object Object]\"")).click();
}catch (Exception A){
System.out.println("DOnt work6");
}
number = number-1;
}
}
}
至 click()
的元素中,占位符为 选择用户名 网址内 https:/mail.protonmail.comcreatenew?语言=en。,因为所需元素在 <iframe>
所以你必须。
您可以使用以下任一方式 定位策略:
使用 cssSelector:
driver.get("https://mail.protonmail.com/create/new?language=en");
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("div.usernameWrap iframe[title='Registration form']")));
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input.input#username"))).click();
使用 xpath:
driver.get("https://mail.protonmail.com/create/new?language=en");
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//div[@class='usernameWrap']//iframe[@title='Registration form']")));
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@class='input' and @id='username']"))).click();
浏览器快照。
你可以找到几个相关的讨论,在。