问题
I'm not able to make it Wright in an username at https://mail.protonmail.com/create/new?language=en. For some reason it cant find or cant klick at "choose username", Can any one help me out?
Code trials:
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;
}
}
}
回答1:
To click()
within the element with placeholder as Choose username within the url https://mail.protonmail.com/create/new?language=en, as the the desired element is within a <iframe>
so you have to:
- Induce WebDriverWait for the desired frameToBeAvailableAndSwitchToIt.
- Induce WebDriverWait for the desired elementToBeClickable.
You can use either of the following Locator Strategies:
Using 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();
Using 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();
Browser Snapshot:
Reference
You can find a couple of relevant discussions in:
- Ways to deal with #document under iframe
- Is it possible to switch to an element in a frame without using driver.switchTo().frame(“frameName”) in Selenium Webdriver Java?
来源:https://stackoverflow.com/questions/62346657/how-to-click-within-the-username-field-within-protonmail-signup-page-using-selen