1

We are working on putting a large amount of string into the input box.

When working with Send_keys(), it takes too long to work.

Here is the method I found to solve this problem.

import pyperclip
from selenium.webdriver.common.keys import Keys
pyperclip.copy('foo')
element.send_keys(Keys.CONTROL, 'v')

This works very effectively, but returns an empty value in headless mode.

Is there any way to fix this in Heldless mode?

Prophet
  • 32,350
  • 22
  • 54
  • 79
anfwkdrn
  • 327
  • 1
  • 7

2 Answers2

1

I see there are already several questions about this issue are existing on Stackoverflow.
First - please try this solution suggesting using klembord instead of pyperclip:

from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from webdriver_manager.firefox import GeckoDriverManager
import klembord
klembord.init()
options = Options()
options.headless = True
driver = webdriver.Firefox(options=options,executable_path=GeckoDriverManager().install())
print("Headless Firefox Initialized. Wait for output")
driver.get("https://www.lipsum.com")
l = driver.find_element_by_xpath("/html/body/div/div[2]/div[2]/div[3]/div[1]/p")
klembord.set_text(l.text) # setting text to clipboard
print("Check clipboard by pressing WIN + V or CTRL +V")
driver.quit()

Another attempt to overcome with issue, but in Java is here:

Prophet
  • 32,350
  • 22
  • 54
  • 79
0

Not sure which browser you are using or platform OS, but generally the OS does not allow headless browsers access/permissions to the OS clipboard or the browser does not have a copy function set in headless mode.

As you have already identified the WebElement, you should try using the Javascript method, so in your case:

driver.execute_script('element.value="TestText";')

or

driver.execute_script('element').setAttribute('value','TestText');

Effectively the Javascript method is passing your content (in example above 'Testtest') into the element you have defined using the .value function.

djmonki
  • 3,020
  • 7
  • 18