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

如何在PyCharm中运行Python机器人脚本并解决Selenium引用问题

Guide to Migrating Your Selenium Bot Script from IDLE to PyCharm

Hey there! Glad you’ve already worked through the unresolved reference issue with Selenium in PyCharm. Let’s walk through the complete process to get your bot running smoothly in PyCharm, step by step:

1. Set Up a New PyCharm Project

  • Launch PyCharm and click New Project.
  • Choose a folder to store your project (pick a location you can easily find later).
  • Critical: Select the same Python interpreter you used when running the script in your terminal. To confirm your terminal’s interpreter:
    • On Windows: Run where python in Command Prompt
    • On macOS/Linux: Run which python in Terminal
    • In PyCharm’s project setup window, click Existing interpreter and browse to that Python executable.

2. Bring in Your Existing Files

  • Copy your main script (the one with the order function), config.py, and chromedriver executable into your new PyCharm project folder.
  • After copying, PyCharm should automatically detect these files—you’ll see them in the Project panel on the left.

3. Verify Selenium Installation

Even though you fixed the unresolved reference issue, double-check that Selenium is installed for your project’s interpreter:

  • Open PyCharm’s built-in Terminal (at the bottom of the window).
  • Run pip install selenium to ensure the package is properly installed. If you use a virtual environment, activate it first in the Terminal (e.g., source venv/bin/activate on macOS/Linux, venv\Scripts\activate on Windows).

4. Run Your Bot Script

You have a few easy ways to launch your script:

  • Click the green play icon that appears next to the if __name__ == '__main__': line in your code.
  • Right-click anywhere in the script editor and select Run 'your_script_name'.
  • Use keyboard shortcuts: Shift+F10 (Windows/Linux) or Control+R (macOS).

5. Optional: Optimize Your Setup

To avoid future ChromeDriver version mismatches, consider using webdriver-manager to auto-manage ChromeDriver:

  1. Install it via PyCharm’s Terminal:
    pip install webdriver-manager
    
  2. Modify your driver initialization code to:
    from webdriver_manager.chrome import ChromeDriverManager
    driver = webdriver.Chrome(ChromeDriverManager().install())
    

This way, you won’t have to manually download and update ChromeDriver every time your Chrome browser updates.

Quick Troubleshooting

  • If you hit import errors again: Go to File > Invalidate Caches... > Invalidate and Restart to refresh PyCharm’s file index.
  • If Chrome won’t launch: Confirm your ChromeDriver version matches your installed Chrome browser (check Chrome’s version via chrome://settings/help and download the matching driver).

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.06 16:29:07