从 Python 通过 Selenium Grid 获取剪贴板的值

huangapple go评论128阅读模式
英文:

Get value of clipboard from a Selenium Grid via Python

问题

我正在使用Selenium for Python连接到我的网格并控制浏览器。
不,我正在尝试获取我的浏览器在网格上的剪贴板数据并将其复制到我的Python代码中。

我尝试过使用from tkinter import Tkclipboard = Tk().clipboard_get(),但显然它复制的是我的主机剪贴板,而不是Selenium网格上的剪贴板。

有办法访问它吗?

英文:

I'm using Selenium for Python to connect to my Grid and command a browser.
No, I'm trying to get the data of the clipboard of my Browser of the grid and copy them into my Python code.

I've tried to use from tkinter import Tk and clipboard = Tk().clipboard_get() but it clearly copy the clipboard on my host and not the one the Selenium Grid

Is there a way to access it?

答案1

得分: 1

我认为您可以使用JavaScript通过驱动程序复制

```python
clipboard_data = driver.execute_script("return await navigator.clipboard.readText();"))

您还需要为您所在的站点授予使用剪贴板的权限,因此在启动驱动程序时,您需要添加以下选项:

options.add_experimental_option("prefs", {"profile.content_settings.exceptions.clipboard": {"[*.]httpbin.org,*": {'last_modified': (time.time()*1000), 'setting': 1}}})

将httpbin替换为您的站点(您还可以使用localhost)。


<details>
<summary>英文:</summary>

I think you can use javascript to copy using the driver:

```python
clipboard_data = driver.execute_script(&quot;return await navigator.clipboard.readText();&quot;))

what you also need to do is give the site that you are on permissions to use the clipboard, so when launching the driver you need to add the following option:

options.add_experimental_option(&quot;prefs&quot;, {&quot;profile.content_settings.exceptions.clipboard&quot;: {&quot;[*.]httpbin.org,*&quot;: {&#39;last_modified&#39;: (time.time()*1000), &#39;setting&#39;: 1}}})

replace httpbin with your site (you can also use localhost).

huangapple
  • 本文由 发表于 2023年3月12日 18:52:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75712609.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定