【问题标题】:Python-Selenium: Clipboard functionality (ctrl + c) not working in Firefox headless modePython-Selenium:剪贴板功能(ctrl + c)在 Firefox 无头模式下不起作用
【发布时间】:2020-04-12 00:27:21
【问题描述】:

我在 firefox 无头模式下使用 ActionChains 函数,但它不起作用。我有这个滚动的 div code_scroll = driver.find_element_by_class_name('CodeMirror-scroll') 我想在这个 div 内双击,然后复制/粘贴整个文本。 double_click() 功能工作正常,我已经对其进行了测试,但我认为 key_down/keyupsend_keys() 功能无法正常工作。底线:文本未被复制。

附:代码在正常模式下运行良好。

驱动配置:

options = webdriver.FirefoxOptions()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
driver = webdriver.Firefox(options=options, capabilities=capa,executable_path=r''+os.path.dirname(os.path.abspath(__file__))+'\geckodriver.exe')

代码:

code_scroll = driver.find_element_by_class_name('CodeMirror-scroll')
actions = ActionChains(driver)
actions.double_click(code_scroll).perform()
actions.key_down(Keys.CONTROL)
actions.send_keys('a').perform()
actions.send_keys('c').perform()
actions.key_up(Keys.CONTROL)
function_body_content = pyperclip.paste()

关于设置窗口的手动大小,我已经尝试了几个答案,但都没有奏效。

已经尝试过的解决方案:

1) driver.set_window_size(1440, 900)
2) options.add_argument("--window-size=1920, 1480")
3) options.add_argument('window-size=1920x1480')
4) options.add_argument('--width=1920')
   options.add_argument('--height=1480')

【问题讨论】:

    标签: python-3.x selenium-webdriver firefox-headless


    【解决方案1】:

    它不起作用的是无头 chrome 或 firefox。似乎这是一种错误。 但是你可以使用 PhantomJs webdriver,它允许剪贴板粘贴,

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-07
      • 2022-12-18
      • 2022-06-30
      • 1970-01-01
      • 2021-06-13
      • 2019-05-13
      相关资源
      最近更新 更多