gpt4 book ai didi

python - 从 PHP 脚本在 Python 中调用 Webdriver 时,Firefox 无法打开

转载 作者:行者123 更新时间:2023-12-05 06:57:10 29 4
gpt4 key购买 nike

我有一个 python 脚本调用 Firefox 的 selenium webdriver。如果我从 CMD 调用 Python 脚本但从简单的 PHP 页面调用则不行。

我尝试了多种方法并简化了我的脚本,但没有成功。

如果我将类似的脚本托管在我的本地 Windows 服务器上,它就可以正常工作。但是,这在托管在 AWS 上时不起作用。

PHP 脚本:

$user_name = 'test';
$short_lang = 'fr';

$pyscript = DIR_PYTHON_SCRIPTS.'retrieve_player_worlds.py';
$python = 'C:\Python39\python.exe';

$cmd = "$python $pyscript $user_name $short_lang";
exec("$cmd", $python_output);

return $python_output;

Python 脚本摘录:

#!C:/Python39/python.exe
import sys
import json

from seleniumwire import webdriver
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver import Firefox
from selenium.webdriver.firefox.options import Options

opts = Options()
opts.log.level = "trace"

try:
binary = FirefoxBinary(r'C:\\Program Files (x86)\\Mozilla Firefox\\Firefox.exe')
driver = webdriver.Firefox(firefox_binary=binary, executable_path='C:\\xampp\\htdocs\\path\\scripts\\python\\geckodriver.exe', options=opts)
driver.get('https://www.google.se') # tried different URLs
driver.maximize_window()

except Exception as e:
logging.exception("Exception occurred binary firefox")

driver.quit()

geckodriver 日志:

606076677289    geckodriver INFO    Listening on 127.0.0.1:50050
1606076680370 mozrunner::runner INFO Running command: "C:\\\\Program Files (x86)\\\\Mozilla Firefox\\\\Firefox.exe" "--marionette" "--headless" "-foreground" "-no-remote" "-profile" "C:\\Windows\\TEMP\\rust_mozprofileQQJMxG"
1606076680370 geckodriver::marionette DEBUG Waiting 60s to connect to browser on 127.0.0.1:50058
*** You are running in headless mode.
1606076685899 Marionette TRACE Marionette enabled
1606076685955 Marionette TRACE Received observer notification toplevel-window-ready
JavaScript error: resource://gre/modules/XULStore.jsm, line 66: Error: Can't find profile directory.
console.warn: SearchSettings: "get: No settings file exists, new profile?" (new Error("", "(unknown module)"))
1606076688307 Marionette TRACE Received observer notification marionette-startup-requested
1606076688308 Marionette TRACE Waiting until startup recorder finished recording startup scripts...
1606076688323 Marionette TRACE All scripts recorded.
1606076688323 Marionette DEBUG Setting recommended pref apz.content_response_timeout to 60000
1606076688323 Marionette DEBUG Setting recommended pref browser.contentblocking.introCount to 99
1606076688323 Marionette DEBUG Setting recommended pref browser.download.panel.shown to true
1606076688323 Marionette DEBUG Setting recommended pref browser.newtabpage.enabled to false
1606076688324 Marionette DEBUG Setting recommended pref browser.pagethumbnails.capturing_disabled to true
1606076688325 Marionette DEBUG Setting recommended pref browser.search.update to false
1606076688325 Marionette DEBUG Setting recommended pref browser.tabs.disableBackgroundZombification to false
1606076688325 Marionette DEBUG Setting recommended pref browser.tabs.remote.separatePrivilegedContentProcess to false
1606076688325 Marionette DEBUG Setting recommended pref browser.tabs.unloadOnLowMemory to false
1606076688325 Marionette DEBUG Setting recommended pref browser.tabs.warnOnCloseOtherTabs to false
1606076688325 Marionette DEBUG Setting recommended pref browser.tabs.warnOnOpen to false
1606076688325 Marionette DEBUG Setting recommended pref browser.usedOnWindows10.introURL to
1606076688325 Marionette DEBUG Setting recommended pref browser.urlbar.suggest.searches to false
1606076688326 Marionette DEBUG Setting recommended pref datareporting.policy.dataSubmissionPolicyAccepted to false
1606076688326 Marionette DEBUG Setting recommended pref dom.disable_beforeunload to true
1606076688326 Marionette DEBUG Setting recommended pref dom.disable_open_during_load to false
1606076688326 Marionette DEBUG Setting recommended pref dom.file.createInChild to true
1606076688326 Marionette DEBUG Setting recommended pref dom.max_chrome_script_run_time to 0
1606076688326 Marionette DEBUG Setting recommended pref dom.max_script_run_time to 0
1606076688327 Marionette DEBUG Setting recommended pref dom.push.connection.enabled to false
1606076688327 Marionette DEBUG Setting recommended pref extensions.getAddons.cache.enabled to false
1606076688327 Marionette DEBUG Setting recommended pref extensions.getAddons.discovery.api_url to data:,
1606076688327 Marionette DEBUG Setting recommended pref network.http.prompt-temp-redirect to false
1606076688327 Marionette DEBUG Setting recommended pref privacy.trackingprotection.enabled to false
1606076688328 Marionette DEBUG Setting recommended pref security.fileuri.strict_origin_policy to false
1606076688328 Marionette DEBUG Setting recommended pref security.notification_enable_delay to 0
1606076688328 Marionette DEBUG Setting recommended pref signon.autofillForms to false
1606076688329 Marionette DEBUG Setting recommended pref signon.rememberSignons to false
1606076688332 Marionette INFO Listening on port 50058
1606076688333 Marionette DEBUG Marionette is listening
1606076688806 Marionette DEBUG Accepted connection 0 from 127.0.0.1:50080
1606076688798 geckodriver::marionette DEBUG Connection to Marionette established on 127.0.0.1:50058.
1606076688822 Marionette DEBUG 0 -> [0,1,"WebDriver:NewSession",{"acceptInsecureCerts":true,"browserName":"firefox","proxy":{"httpProxy":"127.0.0.1:50048","noProxy":[],"proxyType":"manual","sslProxy":"127.0.0.1:50048"}}]
1606076688823 Marionette WARN TLS certificate errors will be ignored for this session
1606076688826 Marionette INFO Proxy settings initialised: {"proxyType":"manual","httpProxy":"127.0.0.1:50048","noProxy":[],"sslProxy":"127.0.0.1:50048"}
1606076688837 Marionette TRACE [21] Frame script loaded
1606076688886 Marionette DEBUG 0 <- [1,1,null,{"sessionId":"f9a971f5-6890-48f2-b7ad-c0d63583e907","capabilities":{"browserName":"firefox","browserVersion":"83.0" ... \rust_mozprofileQQJMxG","moz:shutdownTimeout":60000,"moz:useNonSpecCompliantPointerOrigin":false,"moz:webdriverClick":true}}]
1606076688892 webdriver::server DEBUG <- 200 OK {"value":{"sessionId":"f9a971f5-6890-48f2-b7ad-c0d63583e907","capabilities":{"acceptInsecureCerts":true,"browserName":"firefox","browserVersion":"83.0","moz:accessibilityChecks":false,"moz:buildID":"20201112153044","moz:geckodriverVersion":"0.28.0","moz:headless":true,"moz:processID":7724,"moz:profile":"C:\\Windows\\Temp\\rust_mozprofileQQJMxG","moz:shutdownTimeout":60000,"moz:useNonSpecCompliantPointerOrigin":false,"moz:webdriverClick":true,"pageLoadStrategy":"normal","platformName":"windows","platformVersion":"10.0","proxy":{"httpProxy":"127.0.0.1:50048","noProxy":[],"proxyType":"manual","sslProxy":"127.0.0.1:50048"},"rotatable":false,"setWindowRect":true,"strictFileInteractability":false,"timeouts":{"implicit":0,"pageLoad":300000,"script":30000},"unhandledPromptBehavior":"dismiss and notify"}}}
1606076688892 webdriver::server DEBUG -> POST /session/f9a971f5-6890-48f2-b7ad-c0d63583e907/url {"url": "https://www.google.se"}
1606076688902 Marionette DEBUG 0 -> [0,2,"WebDriver:Navigate",{"url":"https://www.google.se"}]
1606076688916 Marionette TRACE Received message beforeunload for about:blank
1606076691334 Marionette TRACE Received message beforeunload for about:blank
1606076691341 Marionette TRACE Received message pagehide for about:blank
1606076691406 Marionette TRACE Received message DOMContentLoaded for about:neterror?e=nssFailure2&u=https%3A//www.google.se/&c=UTF-8&d=Anslutningen%20till%20www.google.se%20avbr%C3%B6ts%20under%20h%C3%A4mtningen%20av%20sidan.
1606076691407 Marionette DEBUG 0 <- [1,2,{"error":"unknown error","message":"Reached error page: about:neterror?e=nssFailure2&u=https%3A//www.google.se/&c=UTF-8& ... adyState@chrome://marionette/content/navigate.js:60:24\nonNavigation@chrome://marionette/content/navigate.js:285:39\n"},null]
1606076691409 webdriver::server DEBUG <- 500 Internal Server Error {"value":{"error":"unknown error","message":"Reached error page: about:neterror?e=nssFailure2&u=https%3A//www.google.se/&c=UTF-8&d=Anslutningen%20till%20www.google.se%20avbr%C3%B6ts%20under%20h%C3%A4mtningen%20av%20sidan.","stacktrace":"WebDriverError@chrome://marionette/content/error.js:181:5\nUnknownError@chrome://marionette/content/error.js:488:5\ncheckReadyState@chrome://marionette/content/navigate.js:60:24\nonNavigation@chrome://marionette/content/navigate.js:285:39\n"}}
1606076691440 Marionette DEBUG Closed connection 0

我已经用谷歌搜索了几天的错误,但没有任何帮助。我似乎在某个时刻连接中断了。

geckodriver.exe 位于正确的文件夹 (C:\\xampp\\htdocs\\path\\scripts\\python\\) 并且权限似乎没关系,因为日志文件是由脚本创建的。我使用的是最新版本的 Firefox、webdriver (0.28) 和 Selenium。

其他信息(如以下评论中所要求):调用网络驱动程序后,我与页面中的元素进行交互并输入凭据:

iframe = driver.find_element_by_tag_name('iframe')
iframe_switched = driver.switch_to.frame(iframe)

useridInput = driver.find_element_by_id('login_userid')
useridInput.click();

useridInput.send_keys(user_name);

更新 2:

我比较了从 CMD 和从 PHP 页面运行时的 geckodriver 日志。不同的只是最后,其他的(包括命令)都是一样的:

1606076691406   Marionette  TRACE   Received message DOMContentLoaded for about:neterror?e=nssFailure2&u=https%3A//www.google.se/&c=UTF-8&d=Anslutningen%20till%20www.google.se%20avbr%C3%B6ts%20under%20h%C3%A4mtningen%20av%20sidan.
1606076691407 Marionette DEBUG 0 <- [1,2,{"error":"unknown error","message":"Reached error page: about:neterror?e=nssFailure2&u=https%3A//www.google.se/&c=UTF-8& ... adyState@chrome://marionette/content/navigate.js:60:24\nonNavigation@chrome://marionette/content/navigate.js:285:39\n"},null]
1606076691409 webdriver::server DEBUG <- 500 Internal Server Error {"value":{"error":"unknown error","message":"Reached error page: about:neterror?e=nssFailure2&u=https%3A//www.google.se/&c=UTF-8&d=Anslutningen%20till%20www.google.se%20avbr%C3%B6ts%20under%20h%C3%A4mtningen%20av%20sidan.","stacktrace":"WebDriverError@chrome://marionette/content/error.js:181:5\nUnknownError@chrome://marionette/content/error.js:488:5\ncheckReadyState@chrome://marionette/content/navigate.js:60:24\nonNavigation@chrome://marionette/content/navigate.js:285:39\n"}}
1606076691440 Marionette DEBUG Closed connection 0

解码和翻译,这意味着:下载页面时与 www.google.com 的连接被终止

最佳答案

经过一个月的排查,我终于找到了问题所在。如果 Apache 作为 Windows 服务运行,Firefox 似乎不会打开。如果我将 Apache 作为标准应用程序运行,它将正常打开。

我曾尝试让 Apache 服务作为本地系统帐户运行,允许和不允许与桌面交互,以及作为管理员帐户运行,但这没有帮助。

所以最后我卸载了 Apache 服务并将其作为应用程序运行(使用 Xampp)...

关于python - 从 PHP 脚本在 Python 中调用 Webdriver 时,Firefox 无法打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64954592/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com