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

为何Selenium程序在VS Code可运行,在PythonAnywhere却报错?

在PythonAnywhere运行Selenium程序失败的原因及解决办法

问题描述

我在VS Code中能正常运行的Selenium脚本,放到PythonAnywhere上无法执行,代码如下:

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

chromeOptions = webdriver.ChromeOptions()
chromeOptions.add_argument("ignore-certificate-errors")
chromeOptions.add_argument('--headless=chrome')
wbe = webdriver.Chrome('/home/Picen22/ODDS_ACTION/chromedriver', options=chromeOptions)

wbe.get("http://m.diretta.it/?d=0&s=1")
sleep(2)
print(wbe.find_elements(By.XPATH,'//div[@id="score-data"]/a'))

报错信息

/home/Picen22/ODDS_ACTION/prova.py:8: DeprecationWarning: executable_path已被弃用,请传入Service对象
  wbe = webdriver.Chrome('/home/Picen22/ODDS_ACTION/chromedriver', options=chromeOptions)
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/selenium/webdriver/common/service.py", line 71, in start
    self.process = subprocess.Popen(cmd, env=self.env,
  File "/usr/local/lib/python3.10/subprocess.py", line 969, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/usr/local/lib/python3.10/subprocess.py", line 1845, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
PermissionError: [Errno 13] 权限被拒绝: '/home/Picen22/ODDS_ACTION/chromedriver'
在处理上述异常期间,发生了另一个异常:
Traceback (most recent call last):
  File "/home/Picen22/ODDS_ACTION/prova.py", line 8, in <module>
    wbe = webdriver.Chrome('/home/Picen22/ODDS_ACTION/chromedriver', options=chromeOptions)
  File "/usr/local/lib/python3.10/site-packages/selenium/webdriver/chrome/webdriver.py", line 70, in __init__
    super(WebDriver, self).__init__(DesiredCapabilities.CHROME['browserName'], "goog",
  File "/usr/local/lib/python3.10/site-packages/selenium/webdriver/chromium/webdriver.py", line 89, in __init__
    self.service.start()
  File "/usr/local/lib/python3.10/site-packages/selenium/webdriver/common/service.py", line 86, in start
    raise WebDriverException(
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' 可执行文件权限可能有误。

问题原因及解决步骤

核心问题1:chromedriver权限不足

报错里明确提示权限被拒绝,说明当前用户没有执行该chromedriver文件的权限。
解决命令:在PythonAnywhere终端执行以下命令,给驱动文件添加执行权限:

chmod +x /home/Picen22/ODDS_ACTION/chromedriver

核心问题2:Selenium API弃用警告

executable_path参数已被官方弃用,需要改用Service对象管理驱动。
修改后的代码:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service  # 新增导入
from time import sleep
from selenium.webdriver.common.by import By

chromeOptions = webdriver.ChromeOptions()
chromeOptions.add_argument("ignore-certificate-errors")
chromeOptions.add_argument('--headless=chrome')
# 用Service对象指定驱动路径
service = Service('/home/Picen22/ODDS_ACTION/chromedriver')
wbe = webdriver.Chrome(service=service, options=chromeOptions)

wbe.get("http://m.diretta.it/?d=0&s=1")
sleep(2)
print(wbe.find_elements(By.XPATH,'//div[@id="score-data"]/a'))

额外注意

确保上传的chromedriver版本与PythonAnywhere上安装的Chrome版本匹配,可在终端执行google-chrome --version查看Chrome版本,再下载对应版本的驱动上传。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.08 01:30:59