簡體   English   中英

從 PHP 腳本在 Python 中調用 Webdriver 時,Firefox 無法打開

[英]Firefox doesn't open when calling Webdriver in Python from PHP script

我有一個 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。

附加信息(如以下評論中所要求):調用 webdriver 后,我與頁面中的元素進行交互並輸入憑據:

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

解碼和翻譯,這意味着: The connection to www.google.com was terminated while downloading the page

經過一個月的故障排除,我終於找到了問題所在。 如果 Apache 作為 Windows 服務運行,Firefox 似乎不會打開。 如果我將 Apache 作為標准應用程序運行,它將可以正常打開。

我試圖讓 Apache 服務作為本地系統帳戶運行,允許和不允許與桌面交互,並作為管理員帳戶運行,但這無濟於事。

所以最后我卸載了 Apache 服務並將其作為應用程序運行(使用 Xampp)...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM