VietTuts

Tự Học Lập Trình Online

  • Home
  • Java
  • Servlet
  • JSP
  • Struts2
  • Hibernate
  • Spring
  • MyBatis
  • Java WS
  • C
  • C++
  • C#
  • Python
  • PHP
  • Excel
  • VBA
  • Web
    • JavaScript
    • JQUERY
    • JSON
    • AJAX
    • CSS
    • HTML
    • HTML5
    • Node.js
    • Angular 7
  • SQL
    • MySQL
    • SQL Server
  • Misc
    • Eclipse
    • Phần mềm tiện ích
    • Cấu trúc DL&GT
    • Selenium Test

Học Selenium - Automation Test

Kiểm thử tự động là gì? Automation Testing Selenium là gì? Các lệnh trong Selenium IDE – “Selenese” Giới thiệu Selenium Webdriver Selenium WebDriver Locators – xác định vị trí phần tử HTML

Tương Tác Người Dùng

Tương tác người dùng Tương tác Text Box Tương tác Radio Button Tương tác Check Box Tương tác Drop Down Ví dụ kéo thả trong Selenium Thao tác bàn phím Thao tác chuột Chọn nhiều hạng mục Tìm tất cả Link

Selenium WebDriver Nâng Cao

Page Object Model (POM) Selenium WebDriver Page Factory trong Selenium WebDriver XPath trong Selenium WebDriver Inject JQuery trong Selenium WebDriver
1 / 3
❮ ❯

Thao tác với bàn phím trong Selenium


Kéo và thả trong Selenium
Mouse actions trong Selenium

Chúng ta sẽ thực hiện thao tác với bàn phím trong Selenium bằng việc sử dụng phương thức WebElement.sendKeys() hoặc Actions.sendKeys(). Send keys để biểu diễn bàn phím trong trình duyệt. Các phím đặc biệt không phải là văn bản, được biểu thị bằng Khóa được nhận dạng như là một phần của chuỗi ký tự hoặc ký tự riêng lẻ.


Nội dung chính

  • Ví dụ

Ví dụ

Sau đây là ví dụ thực hiện thao tác với bàn phím trong Selenium để mở trang https://google.com, nhập từ khóa 'viettuts.vn' rồi bấm phím Enter để thực hiện tìm kiếm.

1. Sử dụng phương thức Actions.sendKeys():

package vn.viettuts.selenium;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

public class KeyBoardDemo1 {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", 
                "D:\\SeleniumWebdriver\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();

        // Open website
        driver.get("https://google.com");

        // Maximize the browser
        driver.manage().window().maximize();
        
        WebElement searchElement = driver.findElement(By.name("q"));
        searchElement.sendKeys("viettuts.vn");
        Actions action = new Actions(driver);
        action.sendKeys(Keys.RETURN);
        action.perform();
    }
}

Kết quả:

Ví dụ Thao tác với bàn phím trong Selenium

2. Sử dụng phương thức WebElement.sendKeys():

package vn.viettuts.selenium;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class KeyBoardDemo2 {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", 
                "D:\\SeleniumWebdriver\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();

        // Open website
        driver.get("https://google.com");

        // Maximize the browser
        driver.manage().window().maximize();
        
        WebElement searchElement = driver.findElement(By.name("q"));
        searchElement.sendKeys("viettuts.vn", Keys.RETURN);
    }
}

Kết quả:

Ví dụ Thao tác với bàn phím trong Selenium
Kéo và thả trong Selenium
Mouse actions trong Selenium

Recent Updates

Selenium Test là gì? - Kiểm thử tự độngCác lệnh trong Selenium IDE - “Selenese”Chọn nhiều hạng mục trong SeleniumGiới thiệu Selenium WebdriverInject JQuery trong Selenium WebDriverKéo và thả trong SeleniumMouse actions trong SeleniumPage Factory trong SeleniumPage Object Model (POM) trong SeleniumSelenium WebDriver - Tương tác CheckBoxSelenium WebDriver - Tương tác Drop DownSelenium WebDriver - Tương tác người dùngSắp Tết 2026 Rồi! - Còn bao nhiêu ngày nữa là đến tết 2026?

VietTuts on facebook

Học Lập Trình Online Miễn Phí - VietTuts.Vn
Danh Sách Bài Học

Học Java | Hibernate | Spring
Học Excel | Excel VBA
Học Servlet | JSP | Struts2
Học C | C++ | C#
Học Python
Học SQL

Bài Tập Có Lời Giải

Bài tập Java
Bài tập C
Bài tập C++
Bài tập C#
Bài tập Python
Ví dụ Excel VBA

Câu Hỏi Phỏng Vấn

201 câu hỏi phỏng vấn java
25 câu hỏi phỏng vấn servlet
75 câu hỏi phỏng vấn jsp
52 câu hỏi phỏng vấn Hibernate
70 câu hỏi phỏng vấn Spring
57 câu hỏi phỏng vấn SQL

Scroll back to top

Copyright © 2016 VietTuts.Vn all rights reserved. | Liên hệ | Chính sách - riêng tư | sitemap.html | sitemap_index.xml