Using Selenium for Browser Automation

This module provides comprehensive browser automation capabilities using Selenium WebDriver with undetected Chrome support.

Overview

The automation module enables:

  • Undetected Browser Control: Uses undetected_chromedriver to avoid detection
  • Text-based Element Finding: Locate elements by visible text content
  • Visual Element Matching: Use Airtest for image-based element detection
  • Page State Monitoring: Wait for HTML and visual stabilization
  • Document Conversion: Convert Markdown with Ruby annotations to PNG/PDF

Core Components

  • Browser initialization and management
  • Element finding strategies (text-based and visual)
  • Page waiting and monitoring utilities
  • Screenshot and document generation tools

Installation Requirements

Install the required dependencies for browser automation:

pip install undetected_chromedriver webdriver_manager

init[source]

init(*arguments, driver_version:Optional[str]=None, browser_executable_path:Optional[str]=None)

quit[source]

quit()

# from selenium.webdriver.chrome.service import Service
# from webdriver_manager.chrome import ChromeDriverManager

# def init(*arguments):
#     chrome_options = webdriver.ChromeOptions()
#     for argument in arguments:
#         if isinstance(argument, str):
#             chrome_options.add_argument(argument)
#     global driver
#     driver = webdriver.Chrome(
#         options = chrome_options,
#         service = Service(ChromeDriverManager().install())
#     )
#     global device_pixel_ratio
#     device_pixel_ratio = driver.execute_script('return window.devicePixelRatio;')
init('--user-data-dir=C:\\Users\\seii-saintway\\Downloads\\chrome-profile')

Logging Configuration

Set up logging for the automation module to track operations and debug issues.

Text-based Element Detection

This section implements intelligent element finding using visible text content. The system searches through different DOM scopes and supports both exact and partial text matching.

Text-based Browser Automation

Implement browser automation using text-based element detection with enhanced scope searching.

ok[source]

ok()

last[source]

last()

new[source]

new(url)

close[source]

close()

find_elements[source]

find_elements(prompt, exactly=True)

find_elements[source]

find_elements(prompt, exactly=True)

find_element[source]

find_element(prompt, closest_prompt=None)

Page State Monitoring

These utilities help ensure page stability before taking actions or screenshots by monitoring HTML content and visual changes.

click[source]

click(prompt=None, closest_prompt=None, xoffset:int=0, yoffset:int=0)

input[source]

input(text, prompt=None, closest_prompt=None, xoffset:int=0, yoffset:int=0)

Static Page Inspection

Use Selenium for static inspection of page appearance and content monitoring.

get_html_hash[source]

get_html_hash(xpath='//body')

Get the hash of the element's outerHTML.

wait[source]

wait(timeout=inf, stability_duration=1.0, check_interval=0.5, xpath='//body')

Wait until the HTML of the specified element does not change.

Args: timeout: Maximum wait time for stabilization (seconds). stability_duration: Duration for stabilization (seconds). check_interval: Interval to check for changes (seconds). xpath: XPATH of the element to monitor for HTML changes.

Visual Element Detection with Airtest

This section integrates Airtest for image-based element detection, useful when text-based approaches are insufficient.

screen_hash[source]

screen_hash()

Calculate the hash value of the screenshot.

watch[source]

watch(timeout=1.7976931348623157e+308, stability_duration=1.0, check_interval=0.5)

Wait until the screenshot does not change.

Args: timeout: how long to wait for stabilization (seconds) stability_duration: duration for stabilization (seconds) check_interval: check interval (seconds)

Airtest Integration

Use Airtest for advanced browser automation with image-based element detection.

Document Conversion Tools

These tools convert Markdown files with Ruby annotations (for Japanese text) into PNG images and PDF documents using browser rendering.

pip install airtest

try_log_screen[source]

try_log_screen(xpath=None)

find_position[source]

find_position(image, timeout=1.0, threshold=0.9, interval=0.5, intervalfunc=None)

Search for image template in the screen until timeout

Args: image: image file path to be found in screenshot timeout: time interval how long to look for the image template threshold: default is None interval: sleep interval before next attempt to find the image template intervalfunc: function that is executed after unsuccessful attempt to find the image template

Raises: TargetNotFoundError: when image template is not found in screenshot

Returns: TargetNotFoundError if image template not found, otherwise returns the position where the image template has been found in screenshot

inject[source]

inject()

get_mouse_position[source]

get_mouse_position()

move_to_center[source]

move_to_center()

move_and_click[source]

move_and_click(x, y, offset=True)

exists[source]

exists(image)

touch[source]

touch(image, text=None)

fill[source]

fill(text)

Document Conversion

Use Selenium to convert Markdown files with Ruby annotations to PNG and PDF formats.

pip install markdown

convert_md_with_ruby_to_html[source]

convert_md_with_ruby_to_html(md_file_path)

Convert Markdown file containing tags into HTML file

convert_md_content_to_html[source]

convert_md_content_to_html(md_content)

Convert Markdown content containing tags into HTML content

save_file[source]

save_file(file_path, content)

Write the content to a file

convert_html_with_ruby_to_png[source]

convert_html_with_ruby_to_png(html_file_path)

Use Selenium to print the HTML file as PNG

convert_md_with_ruby_to_png[source]

convert_md_with_ruby_to_png(md_file_path)

Convert the Markdown file containing tags into PNG

dialog_for_printing[source]

dialog_for_printing(timeout=inf)

Dialog for printing to PDF

convert_html_with_ruby_to_pdf[source]

convert_html_with_ruby_to_pdf(html_file_path)

Use Selenium to print the HTML file as PDF

convert_md_with_ruby_to_pdf[source]

convert_md_with_ruby_to_pdf(md_file_path)

Convert the Markdown file containing tags into PDF

init('--lang=en')
convert_md_with_ruby_to_png('2024-11-21.md')
convert_md_with_ruby_to_pdf('2024-11-21.md')