You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python Selenium脚本无法运行问题求助

Fixing Your First Selenium Script

Hey Nate, let's work through the bugs in your first Selenium script—you're almost there, just a couple of small syntax issues are holding you back!

Let's break down the problems:

  • Missing parentheses on the click method: When you write driver.find_element_by_id('game').click, you're only referencing the click method instead of executing it. Add parentheses: .click() to actually trigger the click action.
  • Wrong method name for sending keys: In Python's Selenium bindings, the method to input text is send_keys() (all lowercase with an underscore), not sendKeys()—that's Java syntax, which will throw an AttributeError in Python.

Here's your corrected code:

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

# Initialize the Chrome driver
driver = webdriver.Chrome(executable_path="C:\\Users\\natem\\Downloads\\chromedriver_win32\\chromedriver.exe")

# Navigate to the game page
driver.get("https://multiplayer-swordio.sedated.repl.co/")
time.sleep(2)

# Fixed: Added parentheses to .click()
driver.find_element_by_id('game').click()
time.sleep(2)

# Locate input field, interact with it
name = driver.find_element_by_id('input-form')
name.click()
# Fixed: Changed sendKeys to send_keys
name.send_keys("user")
name.send_keys(Keys.ENTER)

time.sleep(20)
# Don't forget to close the driver when finished!
driver.quit()

A quick extra tip: While time.sleep() works for testing, for more reliable scripts, consider using Selenium's explicit waits (with WebDriverWait and expected_conditions) to wait for elements to become interactable instead of relying on fixed sleep times.

内容的提问来源于stack exchange,提问作者Nate Millner

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 22:22:32