如何与两个不同的用户一起运行Selenium Webdriver?
我在Ubuntu服务器上写了一个小的Python脚本,以无头模式运行Chrome浏览器并打开一个URL(请参见下面的脚本)。目前,我的服务器上有两个不同的用户:例如,User1和user2。
我想实现的目标:
- 用user1和user2(两个终端会话)登录
- 从user1启动脚本,当我
- 从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:
- Login with user1 and user2 (two terminal sessions)
- Start the script from user1, when finished
- 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为问题是,只有
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. Youruser2
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 withuser1
. This will make the folder read/writeable to anyone. Then try to run your script withuser2
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.