从apache2启动硒会话



im目前正在尝试开发一个网页,以登录不同网站上的一些帐户。

为此,我创建了一个脚本,它启动了一个硒会话,进入网站并让我登录,一切都很好。

我现在真正的问题在于通过运行在同一服务器上的apache2服务托管的网站来控制会话。

我用这段代码来运行脚本,从这篇文章中重构答案:

<?php
$cmd = "python3 /var/scripts/LoginScript.py yes";
$output = shell_exec($cmd. ' 2>&1 > out.log');
echo $output;
?>

而CCD_ 1是脚本,CCD_。

下面的脚本是登录脚本的第一部分,从命令行手动运行,一切都很好,但一旦我通过php启动它,脚本就只能工作,直到它试图创建驱动程序。(记录的最后一条消息是"也许在这里?"(

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.ui import WebDriverWait
import sys
import os
import dbConn as db
import time
import pickle
print("Starting login process...")
#Get card signature from commandline arguments
card_signature = str(sys.argv[1])
#Get wp-user from provided card signature
wpUser = db.getWPUser(card_signature)
#get example-user from wp-user
user = db.getData(wpUser[0])
print("Connecting to Example, please hold on")
#Start url to connect to (in this case login page from example)
url = "https://example.page.com"
#Setting options for Firefox to run headless and in incognito mode, as well as to ignore 
#certificate errors
options = webdriver.FirefoxOptions()
options.add_argument("--ignore-certificate-errors")
options.add_argument("--headless")
options.add_argument("--incognito")
print("Maybe here?")
#Create driver with the previously set options
driver = webdriver.Firefox(options=options)
print("also here?")
#Go to website
driver.get(url)

dbConn.py是一个简单的数据库访问处理程序,用于获取帐户信息(用户名、密码(

问题的进一步解释以及我迄今为止所做的尝试:

apache服务有足够的权限启动文件(向sudoers添加了测试用例的www数据并修改了文件权限(

正如我之前所说,最后记录的是";也许在这里"消息,我已经尝试将请求超时值提高到120秒,但我不确定这是否正确,因为网站在60到70秒后取消了脚本。

如果我注释掉driver部分,代码运行起来就像黄油一样流畅,只是没有按照我的意愿执行,因为没有真正的selenium会话。

最初我想在屏幕上启动脚本,但当屏幕启动时也没有成功,但脚本不会。(这篇文章并不是一个真正的问题,但可能会以另一种形式发布,因为这是另一个问题——只是为了提供信息而提到它(

如果我用php脚本打开页面,20秒后停止重新加载,输出已经是Maybe here?,但如果脚本在60到70秒后自动取消,输出仍然停留在Maybe here?,那么Apache可能在处理selenium方面有一些问题?这是我唯一的想法,因为我还没有找到合理的方法来记录错误(如果有的话?(

我希望你能帮我!

谢谢(无论如何(。

您需要使用不同的方法来配置选项。将此添加到代码顶部的导入中:

from selenium.webdriver import Firefox
from selenium.webdriver.firefox.options import Options

然后你可以使用:

options = Options()
#You can continue configuring your options as you have been
driver = webdriver.Firefox(executable_path='pathtogeckodriver', options=options)
driver.get(url)

希望这能帮助解决问题。这不应该是apache的问题,但是,如果excetcutable不在同一目录中,或者使用相同的配置创建Firefox实例,apache可能会在查找excetcutaable的路径时遇到问题。

以下是将其添加到路径的更多信息:Selenium使用Python-Geckodriver可执行文件需要在path中,并找到可执行文件的路径:https://askubuntu.com/questions/851401/where-to-find-geckodriver-needed-by-selenium-python-package.

最新更新