selenium - 163 信息 : UPX is not available. Selenium pyinstaller 一个文件.exe

标签 selenium selenium-chromedriver pyinstaller

我已经阅读了几乎所有与此主题相关的帖子,但我找不到解决方案! 我的文件夹路径是:C:\Users\User\Desktop\Data Analytics Arg\py_inst

在我创建虚拟环境的文件夹中,我添加了 chromedriver.exe 和我的脚本,如图所示:

enter image description here

这是我的脚本:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import  expected_conditions as EC
from selenium.webdriver.common.by  import By
import time

def resource_path(relative_path):

try:
    import sys
    import os
    # PyInstaller creates a temp folder and stores path in _MEIPASS
    base_path = sys._MEIPASS
except Exception:
    base_path = os.path.abspath(".")

return os.path.join(base_path, relative_path)

try :

url = "https://pinterest.com"
driver_path = "chromedriver.exe"

#Instanciamos la clase de Options para definir ciertas opciones:
options = Options()
options.add_argument("--lang=es") #lenguaje que queremos utilizar
#options.add_argument("--headless") # utilizar un navegador sin cabeza
options.add_argument("--log-level=3")# omite los warnings en la consola

#definimos ntro.webdriver:

driver = webdriver.Chrome(executable_path=driver_path, options=options)
driver.get(url)

time.sleep(2)

buttons = driver.find_elements_by_css_selector("button[data-test-id='page-scroll-arrow']")
for button in buttons:
    opacity = button.get_attribute("style").split(";")[0][-1]
    if opacity is '1':
        button.click()
    
time.sleep(3)

texto = driver.find_element_by_css_selector('h2.unauth-homepage-signup-title').text
with open('result.txt','w') as file:
    file.write(texto)

driver.close()

except Exception as e: 
    print(e)

我添加到service.py文件(C:\Users\User\Desktop\Data Analytics Arg\py_inst\venv\Lib\site-packages\selenium\webdriver\common\service.py):creationflags= CREATE_NO_WINDOW和从 subprocess 导入 CREATE_NO_ WINDOW ,如下所示:

from subprocess import CREATE_NO_WINDOW, DEVNULL

import errno
import os
import subprocess
from platform import system
from subprocess import PIPE, CREATE_NO_WINDOW
from time import sleep
from selenium.common.exceptions import WebDriverException
from selenium.webdriver.common import utils

和:

self.process = subprocess.Popen(cmd, env=self.env,
                                        close_fds=system() != 'Windows',
                                        stdout=self.log_file,
                                        stderr=self.log_file,
                                        stdin=PIPE,
                                        creationflags=self.creationflags,
                                        creationflags=CREATE_NO_WINDOW)

所有内容均来自此帖子:

hide_chrome_answer

最后,我尝试通过终端创建一个 .exe 文件:

pyinstaller --add-data "chromedriver.exe;." --windowed --onefile prueba_1.py

但是我收到了这个错误:

124 信息:PyInstaller:4.7 124 信息:Python:3.10.0 147 信息:平台:Windows-10-10.0.19042-SP0 149 信息:写入 C:\Users\User\Desktop\Data Analytics Arg\py_inst\prueba_1.spec 153 信息:UPX 不可用。 找不到脚本“C:\Users\User\Desktop\Data Analytics Arg\py_inst\prueba_1.py”

现在我的文件夹是这样的:

enter image description here

如果我运行脚本,在 service.py 发生更改后,我会收到以下消息:

c:\Users\User\Desktop\Data Analytics Arg\py_inst\prueba_script.py:41: SyntaxWarning: 
"is" with a literal. Did you mean "=="?
  if opacity is '1':
Traceback (most recent call last):
  File "c:\Users\User\Desktop\Data Analytics Arg\py_inst\prueba_script.py", line 1, in 
<module>
 from selenium import webdriver
  File "C:\Users\User\Desktop\Data Analytics Arg\py_inst\venv\lib\site- 
  packages\selenium\webdriver\__init__.py", line 18, in <module>
    from .firefox.webdriver import WebDriver as Firefox  # noqa
  File "C:\Users\User\Desktop\Data Analytics Arg\py_inst\venv\lib\site- 
  packages\selenium\webdriver\firefox\webdriver.py", line 31, in <module>
    from .service import DEFAULT_EXECUTABLE_PATH, Service
  File "C:\Users\User\Desktop\Data Analytics Arg\py_inst\venv\lib\site- 
  packages\selenium\webdriver\firefox\service.py", line 20, in <module>
    from selenium.webdriver.common import (service, utils)
  File "C:\Users\User\Desktop\Data Analytics Arg\py_inst\venv\lib\site- 
  packages\selenium\webdriver\common\service.py", line 77
    creationflags=CREATE_NO_WINDOW)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

有人可以帮我吗???.

最佳答案

此错误消息...

UPX is not available

...暗示 pyinstaller 无法找到 upx.exe 来加密 exe 文件。


解决方案

为了修复此错误,您需要根据您的系统配置下载 upx。如果是 64 位 操作系统,您需要下载:

upx-for-windows-10.png

如果您已下载upx并保存在D:\中,您需要执行以下操作:

pyinstaller main.py --key 123456 -n test -F -w --upx-dir D:\

关于selenium - 163 信息 : UPX is not available. Selenium pyinstaller 一个文件.exe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70327138/

相关文章:

python - Pyinstaller 可执行文件不断打开

python - 我们如何在机器人框架中一次通过不同的浏览器

php - php、selenium 和 chromedriver 的 WebdriverCurlException

python - 如何摆脱无法聚焦元素异常

python - 如何将多个元素分配给单个列表变量

javascript - 运行基于 jasmine 和 selenium-webdriver 的测试时出错

python - 如何从 pyinstaller 的输出文件创建 .app?

PHPUnit、Selenium 基本测试失败并出现 fatal error

java - innerhtml 中的对象未被 selenium webdriver 识别

python - 在 mac 上构建适用于 windows .exe 的可执行应用程序