【Python】Operating Web Elements with Selenium: Click and Text Input

目次

Overview

This article explains how to perform user actions like clicking and entering text on web elements (buttons or input forms) obtained using Selenium. These actions—such as entering a keyword in a search box and pressing the search button—are the core of browser automation.

Specifications (Input/Output)

  • Input:
    • Target WebElement object.
    • String to be entered (for send_keys).
  • Output: Click action or text input action on the browser screen.
  • Requirement: Elements must be obtained in advance using methods like find_element.

Basic Usage

This is the basic flow of typing text into an input field and then clicking a button.

Python

# Assuming input_element and button_element have already been obtained

# 1. Enter a string
input_element.send_keys("Python Selenium")

# 2. Click the element
button_element.click()

Full Code

This is a complete working sample that generates a simple HTML page with an input form and a button, then performs “text input” and “clicking.”

from selenium import webdriver
from selenium.webdriver.common.by import By
import time

def operate_elements_demo():
    """
    Demo function for entering text into an input field and clicking a button.
    """
    driver = webdriver.Chrome()

    # Create a test HTML form (input field and button)
    html_content = """
    data:text/html;charset=utf-8,
    <input id='my-input' type='text' placeholder='Enter text here'>
    <button id='my-button' onclick='document.body.style.backgroundColor="yellow"'>Click Change Color</button>
    """

    try:
        # 1. Open the page
        driver.get(html_content)
        
        # 2. Locate and operate on the input field (input tag)
        input_box = driver.find_element(By.ID, "my-input")
        
        print("Entering text...")
        input_box.send_keys("Search keyword")
        time.sleep(2) # For observation

        # 3. Locate and operate on the button (button tag)
        btn = driver.find_element(By.ID, "my-button")
        
        print("Clicking the button...")
        btn.click()
        
        # Verify the result (background color changes to yellow)
        time.sleep(2)
        print("Operation completed")

    except Exception as e:
        print(f"An error occurred: {e}")

    finally:
        driver.quit()

if __name__ == "__main__":
    operate_elements_demo()

Customization Points

Table of Operation Methods

These are the main operation methods you can run on a WebElement object.

MethodDescriptionExample Usage
element.click()Clicks the element.Selecting links, buttons, or checkboxes.
element.send_keys(“string”)Enters a string.Entering data into search boxes or login forms.
element.clear()Clears the input content.Used to reset a value that is already entered.
element.submit()Submits the form.Similar to pressing the Enter key (valid only for form elements).

Special Key Input

send_keys can send not only normal characters but also special keys like Enter or Tab. To use this, import from selenium.webdriver.common.keys import Keys.

from selenium.webdriver.common.keys import Keys

# Enter "Python" and then press the Enter key
element.send_keys("Python")
element.send_keys(Keys.ENTER)

Important Notes

Is the element interactable?

Even if an element is visible, an error (ElementClickInterceptedException) occurs if another element (like an ad or popup) overlaps it when you call click(). In such cases, you need to close the overlapping element or use JavaScript to force the click.

Clearing before Input

If you use send_keys on an input field that already contains text, the string will be appended. If you want to replace the input value completely, execute element.clear() first.

Elements out of View

Generally, Selenium automatically scrolls to display an element before operating on it. However, if this fails, you may need to scroll explicitly using a script: driver.execute_script("arguments[0].scrollIntoView();", element).

Advanced Usage

This is a practical pattern that goes from entering a keyword to executing a search on a top-level search site.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time

def google_search_demo():
    driver = webdriver.Chrome()
    driver.get("https://www.google.com")
    
    try:
        # Find the search box (name="q" is standard for Google)
        search_box = driver.find_element(By.NAME, "q")
        
        # Enter keyword and press the Enter key immediately
        # This is more reliable and faster than clicking a search button
        print("Searching...")
        search_box.send_keys("Python Selenium Tutorial")
        search_box.send_keys(Keys.ENTER)
        
        time.sleep(3) # Wait to see the results
        print(f"Search Results Page: {driver.title}")

    finally:
        driver.quit()

if __name__ == "__main__":
    google_search_demo()

Summary

The basics of screen operation are “Locate (find_element)” followed by “Input (send_keys)” or “Click (click)”. Especially for form inputs, combining clear() to delete existing values and Keys.ENTER for confirmation allows you to accurately replicate human-like actions.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次