如何使用Selenium和Java在ProtonMail注册页面的用户名字段内单击 [英] How to click within the username field within ProtonMail signup page using Selenium and Java

查看:92
本文介绍了如何使用Selenium和Java在ProtonMail注册页面的用户名字段内单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使用

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

    解决方案

    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:

    这篇关于如何使用Selenium和Java在ProtonMail注册页面的用户名字段内单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆