【问题标题】:Starting selenium session from apache2从 apache2 开始 selenium 会话
【发布时间】:2022-01-11 17:02:13
【问题描述】:

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

为此,我创建了一个脚本,它启动一个 selenium 会话,进入网站并让我登录,一切正常。

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

我使用此代码运行脚本,重构自 this 帖子答案:

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

/var/scripts/LoginScript.py 是脚本,yes 是必要参数。

以下脚本是登录脚本的第一部分,从命令行手动运行一切正常,但一旦我通过 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服务有足够的权限来启动文件(将测试用例的www-data添加到sudoers并修改文件权限)

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

如果我注释掉 driver 部分,代码运行起来就像黄油一样流畅,除了没有做我想做的事情,因为没有真正的 selenium 会话。

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

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

希望你能帮帮我!

谢谢(无论如何)。

【问题讨论】:

    标签: python php selenium ubuntu apache2


    【解决方案1】:

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

    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='\path\to\geckodriver', options=options)
    driver.get(url)
    

    希望这有助于解决问题。这应该不是 apache 的问题,但是如果 apache 不在同一目录中或使用相同的配置创建 Firefox 实例,则 apache 可能无法找到可执行文件的路径。

    这里有更多关于将其添加到路径的信息:Selenium using Python - Geckodriver executable needs to be in PATH 并找到可执行文件的路径:https://askubuntu.com/questions/851401/where-to-find-geckodriver-needed-by-selenium-python-package

    【讨论】:

    • 抱歉,回答迟了,但有些问题让我无法测试这个解决方案......遗憾的是它不起作用,对于我现在的状态,并没有改变任何东西。现在我又卡在了和以前一样的地方,评论“也许在这里?”作为运行整整一分钟之前的最后一件事并且刷新超时。作为参考,这是我现在创建驱动程序的代码:options = Options() options.add_argument("--ignore-certificate-errors") options.add_argument("--headless") driver = webdriver.Firefox(executable_path='/usr/local/bin/geckodriver', options=options)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-03
    • 1970-01-01
    • 1970-01-01
    • 2010-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多