gpt4 book ai didi

python - 从 apache2 开始 selenium session

转载 作者:行者123 更新时间:2023-12-04 18:48:23 25 4
gpt4 key购买 nike

我目前正在尝试开发一个网页来登录不同网站上的一些帐户。
为此,我创建了一个脚本,它启动一个 selenium session ,进入网站并让我登录,一切正常。
我现在真正的问题在于通过在同一台服务器上运行的 apache2 服务托管的网站来控制该 session 。
我使用此代码运行脚本,重构自 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 session 。
最初我想在屏幕中启动脚本,但在屏幕启动时也没有成功,但脚本不会。 (对于这篇文章来说并不是一个真正的问题,但可能会以另一种形式发布,因为这是另一个问题 - 只是为了提供信息而提到它)
如果我使用 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='\path\to\geckodriver', options=options)
driver.get(url)
希望这有助于解决问题。这不应该是 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 .

关于python - 从 apache2 开始 selenium session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70246299/

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