如何与两个不同的用户一起运行Selenium Webdriver?

发布于 2025-02-07 13:54:47 字数 1187 浏览 2 评论 0原文

我在Ubuntu服务器上写了一个小的Python脚本,以无头模式运行Chrome浏览器并打开一个URL(请参见下面的脚本)。目前,我的服务器上有两个不同的用户:例如,User1和user2。

我想实现的目标:

  1. 用user1和user2(两个终端会话)登录
  2. 从user1启动脚本,当我
  3. 从user2启动脚本

时,如果我使用user1启动脚本,则一切正常。

import sys
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service

def getChromeDriver():
    options = Options()
    options.add_argument("--no-sandbox")
    options.add_argument("--disable-dev-shm-usage")
    options.add_argument("--headless")
    options.add_argument("start-maximized")
    options.add_argument("window-size=1900,1080")
    s = Service("/home/dev/chromedriver")
    driver = webdriver.Chrome(options=options, service=s)
    return driver

driver = getChromeDriver()
driver.get("https://www.google.com")

time.sleep(3)
driver.quit()

当我使用su -user2切换到user2并启动脚本时,我会收到错误消息:devToolSactiveport文件不存在。我需要重新启动服务器以使脚本从user2执行。但是,我无法执行User1(同一问题)的脚本。奇怪的是,当我将脚本作为root(无重新启动)运行时,它起作用。

因此,简而言之:似乎我只能与用户一起运行脚本,该用户在服务器重新启动后首次执行脚本。

任何帮助或提示都将受到赞赏。

问候,德博

I wrote a small python script on my Ubuntu server to run a chrome browser in headless mode and open a url (see the script below). Currently I have two different users on my server: Let’s say user1 and user2.

What I want to achieve:

  1. Login with user1 and user2 (two terminal sessions)
  2. Start the script from user1, when finished
  3. Start the script from user2

Everything works fine if I start the script with user1.

import sys
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service

def getChromeDriver():
    options = Options()
    options.add_argument("--no-sandbox")
    options.add_argument("--disable-dev-shm-usage")
    options.add_argument("--headless")
    options.add_argument("start-maximized")
    options.add_argument("window-size=1900,1080")
    s = Service("/home/dev/chromedriver")
    driver = webdriver.Chrome(options=options, service=s)
    return driver

driver = getChromeDriver()
driver.get("https://www.google.com")

time.sleep(3)
driver.quit()

When I switch to user2 with su - user2 and start the script I get the error message: DevToolsActivePort file doesn't exist. I need to restart the server to get the script executed from user2. But then I can’t execute the Script from user1 (same problem). Curiously, it works when I run the script as root (without restart).

So in short: It seems like I can only run the script with the user that executed the script for the first time after a server restart.

Any help or hints are appreciated.

Regards, Debo

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

愛上了 2025-02-14 13:54:47

我认为问题是,只有user1/tmp/crastpad上具有权利,因为该用户首先创建了该文件夹。 root可以在任何地方读/写,因此使用root用户。您的user2无法从/到/tmp/crastpad读取/写入,这导致了错误消息。

要检查用户特定的TMP DIR是否可以解决您的问题,您可以发行chmod -r 777/tmp/crastpad在使用user1运行脚本后。这将使任何人都可以阅读/写入文件夹。然后尝试使用user2运行您的脚本,并且应该使用。

在Ubuntu上,您可以通过添加行来设置用户特定的TMP DIR
导出tmpdir = $ home/tmp
.profile.bashrc位于用户$ home文件夹中的文件。

I think the problem is that only user1 has rights on /tmp/Crashpad, because this user created the folder in the first place. root can read/write anywhere, so with the root user it works. Your user2 cannot read/write from/to /tmp/Crashpad and this leads to the error message.

To check if user specific tmp dirs will resolve your problem, you can issue chmod -R 777 /tmp/Crashpad after running the script with user1. This will make the folder read/writeable to anyone. Then try to run your script with user2 and it should work.

On Ubuntu, you can set user specific tmp dirs by adding the line
export TMPDIR=$HOME/tmp
to the .profile or the .bashrc file located in the users $HOME folder.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文