Python selenium 没有正确执行书签
Python selenium not executing bookmarklet properly
我有一个从浏览器成功执行的小书签:
javascript:!function(a){var b=document.createElement("textarea"),c=document.getSelection();b.textContent= a,document.body.appendChild(b),c.removeAllRanges(),b.select(),document.execCommand("copy"),c.removeAllRanges(),document.body.removeChild(b);}(<text here>);
当我尝试使用 Python 通过 Selenium webdriver 执行此操作时,它返回 None。关于如何让这个像书签一样将文本复制到剪贴板的任何想法?完整代码如下:
from selenium import webdriver
chromeOptions = webdriver.ChromeOptions()
chromeOptions.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=chromeOptions, desired_capabilities=chromeOptions.to_capabilities())
driver.implicitly_wait(5)
driver.get("websitehere")
js = """javascript:!function(a){var b=document.createElement("textarea"),c=document.getSelection();b.textContent= a,document.body.appendChild(b),c.removeAllRanges(),b.select(),document.execCommand("copy"),c.removeAllRanges(),document.body.removeChild(b);}(<texthere>);
"""
print(driver.execute_script(js))
在网上阅读了一些内容后,由于安全问题,此类工作流程似乎已被阻止。我在控制台中看到了这个错误 chrome document.execCommand(‘cut’/‘copy’) was denied because it was not called from inside a short running user-generated event handler.
请注意,以下内容仅适用于 Firefox Chrome,在接下来的几个版本中,Chrome 可能会开始失败。
from selenium import webdriver
import time
import pyperclip
import pyautogui
driver = webdriver.Chrome(executable_path=r'C:\Path\To\chromedriver.exe')
driver.get("https://www.google.com")
time.sleep(2)
pyautogui.hotkey('ctrl', 'shift', 'j')
time.sleep(2)
js2 = """
var testCopy = function(a) {
var b=document.createElement("textarea"), c=document.getSelection();
b.textContent = a,
document.body.appendChild(b)
c.removeAllRanges()
b.setAttribute("id", "testid")
b.select()
document.execCommand("copy")
console.log('copy success', document.execCommand('copy'));
c.removeAllRanges();
}
testCopy("ThisText")
"""
driver.execute_script(js2)
time.sleep(1)
a = pyperclip.paste()
print(a)
我有一个从浏览器成功执行的小书签:
javascript:!function(a){var b=document.createElement("textarea"),c=document.getSelection();b.textContent= a,document.body.appendChild(b),c.removeAllRanges(),b.select(),document.execCommand("copy"),c.removeAllRanges(),document.body.removeChild(b);}(<text here>);
当我尝试使用 Python 通过 Selenium webdriver 执行此操作时,它返回 None。关于如何让这个像书签一样将文本复制到剪贴板的任何想法?完整代码如下:
from selenium import webdriver
chromeOptions = webdriver.ChromeOptions()
chromeOptions.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=chromeOptions, desired_capabilities=chromeOptions.to_capabilities())
driver.implicitly_wait(5)
driver.get("websitehere")
js = """javascript:!function(a){var b=document.createElement("textarea"),c=document.getSelection();b.textContent= a,document.body.appendChild(b),c.removeAllRanges(),b.select(),document.execCommand("copy"),c.removeAllRanges(),document.body.removeChild(b);}(<texthere>);
"""
print(driver.execute_script(js))
在网上阅读了一些内容后,由于安全问题,此类工作流程似乎已被阻止。我在控制台中看到了这个错误 chrome document.execCommand(‘cut’/‘copy’) was denied because it was not called from inside a short running user-generated event handler.
请注意,以下内容仅适用于 Firefox Chrome,在接下来的几个版本中,Chrome 可能会开始失败。
from selenium import webdriver
import time
import pyperclip
import pyautogui
driver = webdriver.Chrome(executable_path=r'C:\Path\To\chromedriver.exe')
driver.get("https://www.google.com")
time.sleep(2)
pyautogui.hotkey('ctrl', 'shift', 'j')
time.sleep(2)
js2 = """
var testCopy = function(a) {
var b=document.createElement("textarea"), c=document.getSelection();
b.textContent = a,
document.body.appendChild(b)
c.removeAllRanges()
b.setAttribute("id", "testid")
b.select()
document.execCommand("copy")
console.log('copy success', document.execCommand('copy'));
c.removeAllRanges();
}
testCopy("ThisText")
"""
driver.execute_script(js2)
time.sleep(1)
a = pyperclip.paste()
print(a)