Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gui_press_key() or gui_press_keys() doesn't work #3944

Closed
Labels
UC Mode / CDP ModeUndetected Chromedriver Mode / CDP Mode can't reproduceWe tried to see what you saw, but didn't
@tom63001

Description

 import asyncio
 import subprocess
 import time
 from pathlib import Path
 from seleniumbase.core import sb_cdp
 from seleniumbase.undetected.cdp_driver import cdp_util
 try:
 host = "127.0.0.1"
 port = 9222
 driver = None
 loop = None
 print("Connecting to Chrome at localhost:9222...")
 driver = loop.run_until_complete(cdp_util.start(host=host, port=port))
 print(f"Loading: {file_url}")
 page = loop.run_until_complete(driver.get(file_url))
 sb = sb_cdp.CDPMethods(loop, page, driver)
 sb.sleep(2)
 print("Navigating to Google...")
 page = loop.run_until_complete(driver.get("https://google.com"))
 sb = sb_cdp.CDPMethods(loop, page, driver)
 sb.sleep(3)
 
 # reobtain window id, as it could be changed
 window_id = get_chrome_window_id()
 if window_id:
 print(f"Refocusing Chrome window: {window_id}")
 focus_chrome_window()
 
 # find search box
 search_box = None
 selectors = [
 'textarea[name="q"]',
 'input[name="q"]',
 'textarea.gLFyf',
 'input.gLFyf',
 '#APjFqb'
 ]
 
 for selector in selectors:
 if sb.is_element_present(selector):
 search_box = selector
 print(f"Found search box: {selector}")
 break
 
 if search_box:
 # method A: sb.type()
 print("\nMethod A: Using sb.type()...")
 sb.click(search_box)
 sb.sleep(0.5)
 
 # clear
 sb.evaluate(f"document.querySelector('{search_box}').value = ''")
 
 test_text = "selenium test"
 sb.type(search_box, test_text)
 sb.sleep(1)
 
 value = sb.evaluate(f"document.querySelector('{search_box}').value")
 if value == test_text:
 print(f"✅ sb.type() works on Google! Value: '{value}'")
 else:
 print(f"❌ sb.type() failed on Google. Expected '{test_text}', got '{value}'")
 
 # method B: xdotool
 print("\nMethod B: Using xdotool (improved)...")
 sb.evaluate(f"document.querySelector('{search_box}').value = ''")
 sb.click(search_box)
 sb.sleep(0.5)
 
 # keep focus
 if window_id:
 subprocess.run(['xdotool', 'windowfocus', window_id], check=False)
 time.sleep(0.2)
 
 test_text = "xdotool test"
 xdotool_type_to_window(test_text, window_id)
 time.sleep(1)
 
 value = sb.evaluate(f"document.querySelector('{search_box}').value")
 if value == test_text:
 print(f"✅ xdotool works on Google! Value: '{value}'")
 else:
 print(f"❌ xdotool failed on Google. Expected '{test_text}', got '{value}'")
 
 # method C: gui_press_keys
 print("\nMethod C: Using gui_press_keys...")
 sb.evaluate(f"document.querySelector('{search_box}').value = ''")
 sb.click(search_box)
 sb.sleep(0.5)
 
 test_text = "pyautogui"
 print(f"Typing with gui_press_keys: '{test_text}'")
 try:
 sb.gui_press_keys(list(test_text))
 sb.sleep(1)
 
 value = sb.evaluate(f"document.querySelector('{search_box}').value")
 if value == test_text:
 print(f"✅ gui_press_keys works on Google! Value: '{value}'")
 else:
 print(f"❌ gui_press_keys failed. Expected '{test_text}', got '{value}'")
 except Exception as e:
 print(f"❌ gui_press_keys failed with error: {e}")
 else:
 print("❌ Could not find Google search box")
 except:
 pass

tested on:
os: ubuntu 24.04 LTS
environment: chrome and chromium 134
seleniumbase ver: 4.39.4
pyautogui ver: 0.9.54
xdotool ver: 3.2

the search box simply didn't have any inputs at all, by using gui_press_keys or using pyautogui directly.
i have also tried pynput as well, also doesn't work.
it looks like as long as the cdp driver is attached to the browser, all ime events get blocked

Metadata

Metadata

Assignees

No one assigned

    Labels

    UC Mode / CDP ModeUndetected Chromedriver Mode / CDP Mode can't reproduceWe tried to see what you saw, but didn't

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

        AltStyle によって変換されたページ (->オリジナル) /