如何使用Selenium和Java在ProtonMail注册页面的用户名字段中单击

如何使用Selenium和Java在ProtonMail注册页面的用户名字段中单击,java,selenium,xpath,iframe,css-selectors,Java,Selenium,Xpath,Iframe,Css Selectors,我不能让它在一个用户名莱特在。由于某些原因,它找不到或无法点击“选择用户名”,有人能帮我吗 代码试用: public class LaunchApplication { public static void main(String[] args) throws InterruptedException { System.setProperty("webdriver.chrome.driver", "C:\\chromedriver_win32\\chromedriver

我不能让它在一个用户名莱特在。由于某些原因,它找不到或无法点击“选择用户名”,有人能帮我吗

代码试用:

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;
        }





    }
}
公共类启动应用程序{
公共静态void main(字符串[]args)引发InterruptedException{
System.setProperty(“webdriver.chrome.driver”,“C:\\chromedriver\U win32\\chromedriver.exe”);
WebDriver驱动程序=新的ChromeDriver();
driver.manage().window().maximize();
整数=1;
字符串url=”https://protonmail.com";
字符串password=“passwordpassword123123”;
字符串username=“”;
String characters=“abcdefghijklmnopqrstuvxyzabefghijklmnopqrstuvxyz”;
Random rand=新的Random();
整数长度=8;
而(数量>0){
//为用户名创建一个随机字符串:
//如何制作字符串随机器的视频https://www.youtube.com/watch?v=CZYeTblcOU8 过来看
字符[]文本=新字符[长度];
for(int i=0;i
单击()
,请在元素中使用占位符作为url中的“选择用户名”,因为所需元素位于
中,因此您必须:

  • 诱导WebDriverWait使所需的框架可用并切换到它
  • 将所需元素的WebDriverWait诱导为可禁用
  • 您可以使用以下任一选项:

    • 使用cssSelector:

    • 使用xpath:

  • 浏览器快照:


参考文献 您可以在以下内容中找到一些相关讨论:


很肯定ProtonMail不希望您这样做。您可能不希望,但这只是为了学习非常感谢!!那太好了!我对Selenium有点陌生,但我不会完全了解:)
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();
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();