首页 文章

无法找到密码输入对象使用selenium登录谷歌

提问于
浏览
-1

我在selenium中使用 PhantomJS 作为驱动程序 . 在这里,我发现插入电子邮件地址没有问题 . 但是,单击下一个后,应该有一个名为'password'的输入标记 . 但是,在等待之后,我无法获得所需的'password'标签 . 它显示错误没有找到元素 . 有时,我收到以下错误(显示堆栈跟踪):

org.openqa.selenium.InvalidElementStateException: {"errorMessage":"Element is not currently interactable and may not be manipulated","request":{"headers":{"Accept-Encoding":"

因为,我等了足够的时间,以下代码必须工作 . 以下是工作的代码段 .

driver.get("https://accounts.google.com/ServiceLoginAuth");   
    driver.findElement(By.id("identifierId")).sendKeys("xxxxxxxx");
    driver.findElement(By.id("identifierNext")).click();

    WebDriverWait wait = new WebDriverWait(driver, 20);
    wait.until(ExpectedConditions.presenceOfElementLocated(By.name("password"))); 
    driver.findElement(By.name("password")).sendKeys("xxxxxxxx");
    driver.findElement(By.id("passwordNext")).click();

以下是显示google sign in page电子邮件输入字段的特定html:

input type="email" class="whsOnd zHQkBf" jsname="YPqjbf" autocomplete="username" spellcheck="false" tabindex="0" aria-label="Email or phone" name="identifier" id="identifierId" dir="ltr" data-initial-dir="ltr" data-initial-value=""

Next 按钮的ID为“ identifierNext ”,隐藏密码字段为:

input type="password" name="hiddenPassword" jsname="RHeR4d" class="yb9KU" tabindex="-1" aria-hidden="true"

插入电子邮件并单击下一步后,密码的输入字段为:

input type="password" class="whsOnd zHQkBf" jsname="YPqjbf" autocomplete="current-password" spellcheck="false" tabindex="0" aria-label="Enter your password" name="password" autocapitalize="off" autocorrect="off" dir="ltr" data-initial-dir="ltr" data-initial-value=""

现在,问题是,我使用等待机制重新加载页面,以便我可以获得密码插入页面 . 但是,使用Selenium我找不到'password'的命名标签 . 我想 click is not working properly . 是否有机会不对 Next 点击?

2 回答

  • 0

    试试这个:

    driver.findElement(By.xpath("//input[1]"));
    

    或这个:

    WebDriverWait wait = new WebDriverWait(driver, 100);
    wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//input[@type='password']")));
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[@type='password']")));
    WebElement elementpwd = driver.findElement(By.xpath("//input[@type='password']"));
    

    我希望它有所帮助 .

  • 0

    请使用chrome浏览器找到使用selenium web驱动程序发送电子邮件的完整代码

    公共课Newtest {

    public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub
    
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\Tharun\\Desktop\\work\\chromedriver.exe");
        WebDriver driver= new ChromeDriver();
        driver.get("https://www.google.com");
        driver.findElement(By.xpath("//*[@id='gb_70']")).click();
        driver.findElement(By.xpath("//*[@id=\'identifierId\']")).sendKeys("*******@gmail.com");
        driver.findElement(By.xpath("//*[@id=\'identifierNext\']/content/span")).click();
        Thread.sleep(2000);
        driver.findElement(By.xpath("//input[@type='password']")).sendKeys("*******");
        driver.findElement(By.xpath("//*[@id=\'passwordNext\']/content/span")).click();
        Thread.sleep(2000);
        driver.findElement(By.xpath("//*[@id=\"gbw\"]/div/div/div[1]/div[2]/a")).click();
        Thread.sleep(2000);
        driver.findElement(By.xpath("//*[@id=':ir']/div/div")).click();
        Thread.sleep(5000);
        driver.findElement(By.cssSelector("#\\3a og")).sendKeys("*******@gmail.com");
        driver.findElement(By.cssSelector("#\\3a ny")).sendKeys("This is my selenium web driver automation code ");
        Thread.sleep(5000);
        driver.findElement(By.xpath("//*[@id=\':p3\']")).sendKeys("This is my selenium web driver automation code ");
        Thread.sleep(2000);
        driver.findElement(By.xpath("//*[@id=\':no\']")).click();
    
    
    
    
    }
    

    }

相关问题