简体   繁体   English

在Selenium Webdriver中,在文本框中输入文本后单击按钮

[英]In Selenium Webdriver, click button once the text is entered in text box

I am trying to build a Selenium Automation framework for Gmail. 我正在尝试为Gmail构建Selenium自动化框架。 I have installed below tools: JDK, Eclipse, Selenium Jars, Gradle, TestNG 我安装了以下工具:JDK,Eclipse,Selenium Jars,Gradle,TestNG

I am trying to login to gmail. 我正在尝试登录gmail。 But i cam getting below error by the time i enter my username. 但是当我输入用户名时,我会低于错误。 Its trying to click "Next" button before username is entered. 它尝试在输入用户名之前单击“下一步”按钮。

Can I use wait where ever required while developing framework? 我可以在开发框架时使用wait地方吗? Do I need to maintain any standards while calling wait . 在呼叫wait我是否需要保持任何标准。 Write any user defined wait methods. 编写任何用户定义的wait方法。

Error: FAILED: gmailLoginShouldBeSuccessful org.openqa.selenium.ElementNotVisibleException: Cannot click on element (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 207 milliseconds 错误:FAILED:gmailLoginShouldBeSuccessful org.openqa.selenium.ElementNotVisibleException:无法单击元素(警告:服务器未提供任何堆栈跟踪信息)命令持续时间或超时:207毫秒

My Code: 我的代码:

@Test
public void gmailLoginShouldBeSuccessful(){
    //1.Go to Gmail website
    System.setProperty("webdriver.ie.driver", "C:\\Selenium_Softwares_Docs_Videos\\IEDriverServer_x64_3.1.0\\IEDriverServer.exe");
    WebDriver driver = new InternetExplorerDriver();
    driver.manage().deleteAllCookies();
    driver.manage().window().maximize();
    driver.get("http://gmail.com");     
    //2.Fill in username
    WebElement userTextBox = driver.findElement(By.id("Email"));
    userTextBox.clear();
    userTextBox.sendKeys("xxxx");
    //3. click on next button
    WebElement nxtBtn = driver.findElement(By.id("next"));
    nxtBtn.click();
    //4.Fill in password
    WebElement pwdTextBox = driver.findElement(By.id("Passwd-hidden"));
    userTextBox.clear();
    userTextBox.sendKeys("xxxxxxx");
    //5.Click sign in
    WebElement signBtn = driver.findElement(By.id("signIn"));
    signBtn.click();        
}

You can use explicit wait to achieve your requirement. 您可以使用显式等待来满足您的要求。

WebDriverWait wait = new WebDriverWait(yourWebDriver, 5);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//xpath_to_element")));

Webdriver will wait for 5 seconds for your element to be able to be clicked. Webdriver将等待5秒钟,以便能够单击您的元素。

Use Chrome Driver instead of internet Explorer: 使用Chrome驱动程序代替Internet Explorer:

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

public class TestCommons {
    @Test
    public void gmailLoginShouldBeSuccessful() throws InterruptedException {
        // 1.Go to Gmail website
        System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.manage().window().maximize();
        driver.get("http://gmail.com");
        // 2.Fill in username
        driver.findElement(By.id("Email")).clear();
        driver.findElement(By.id("Email")).sendKeys("vishala");
        // 3. click on next button
        driver.findElement(By.id("next")).click();
        // 4.Fill in password
        driver.findElement(By.id("Passwd")).sendKeys("vishala");
        // 5.Click sign in
        driver.findElement(By.id("signIn")).click();
        driver.quit();
    }
}

Hope this will work for you :) 希望这对你有用:)

您是否可以发送RETURN键而不是单击“登录”按钮?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Eclipse 中使用 Selenium WebDriver 和 java 代码在搜索字段中输入文本后如何按“Enter” - How to press 'Enter' once text is entered in the search field using Selenium WebDriver and java code in eclipse 如何使用 Selenium WebDriver 单击按钮并删除输入文本 - How to click button and delete input text with Selenium WebDriver Selenium Webdriver如何在iframe所见即所得文本区域中输入文本 - selenium webdriver how to get text entered in iframe wysiwyg text area Selenium Webdriver处理Java脚本文本框 - Selenium Webdriver to handle java script text box Selenium Webdriver - 跳转到下一个字段时输入的文本消失 - Selenium Webdriver - The entered text is vanished when jumping to the next field 如何在Selenium WebDriver中单击链接文本 - How to click link text in selenium webdriver 使用Robot类和Selenium WebDriver将大写文本发送到文本框 - Send caps text to a text box using Robot class & Selenium WebDriver 如何在Selenium Webdriver的多个文本框中插入值 - how to insert value in multiple text box in selenium webdriver 如何根据垂直参数在硒webdriver的文本框中插入值 - how to insert value in text box in selenium webdriver according to perticular parameter 如何将文本发送到使用硒webdriver中的自动完成功能的搜索框 - How to send text to the search box which uses autocomplete in selenium webdriver
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM