The idea would be to literally do the same keyboard work:
Step 1 -> Open Web Page
Step 2 -> Ctrl + A (Select All)
Step 3 -> Ctrl + C (Copy)
Here's how I use it for Chrome
:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import pyperclip
import time
option = webdriver.ChromeOptions()
option.add_argument('headless')
driver = webdriver.Chrome(options=option)
link='https://sports.staticcache.org/scoreboards/scoreboards-football/index.html?eventId=21150687'
driver.get(link)
time.sleep(10)
element=driver.find_element_by_tag_name('body')
element.send_keys(Keys.CONTROL,'a')
element.send_keys(Keys.CONTROL,'c')
driver.quit()
alltext=pyperclip.paste()
print(alltext)
Here's how I use it for Firefox
:
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.keys import Keys
import pyperclip
import time
option = Options()
option.headless = True
driver = webdriver.Firefox()
link='https://sports.staticcache.org/scoreboards/scoreboards-football/index.html?eventId=21150687'
driver.get(link)
time.sleep(10)
element=driver.find_element_by_tag_name('body')
element.send_keys(Keys.CONTROL,'a')
element.send_keys(Keys.CONTROL,'c')
driver.quit()
alltext=pyperclip.paste()
print(alltext)
In both options when Headless
is not activated, it works perfectly, but when it is activated nothing happens and the script finishes running without anything being delivered.
Is there anything I can do to resolve this?
As you are trying to just print the content of the page, So instead of using send_keys
you can try with the text
option = webdriver.ChromeOptions()
option.add_argument('headless')
driver = webdriver.Chrome("<Path of chromeDriver>",chrome_options=option)
link='https://sports.staticcache.org/scoreboards/scoreboards-
football/index.html?eventId=21150687'
driver.get(link)
time.sleep(10)
element=driver.find_element_by_tag_name('body')
print(element.text)