λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
πŸ‘¨πŸΌ‍πŸ’»κ°œλ°œ/μ…€λ ˆλ‹ˆμ›€

μ…€λ ˆλ‹ˆμ›€ - html μ„œμ‹λ„ ν¬ν•¨ν•΄μ„œ ν…μŠ€νŠΈ λ³΅μ‚¬ν•˜κΈ°(klembord)

by Janger 2023. 7. 23.
728x90
λ°˜μ‘ν˜•

 

example.py
import time
import klembord
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains

options = Options()
options.add_argument("--headless")
options.add_argument("--disable-gpu")
options.add_argument("--no-sandbox")
options.add_argument("--disable-dev-shm-usage")

driver = webdriver.Chrome(options=options)
driver.get("https://example.com/")
time.sleep(2) 

element = driver.find_element("xpath", "/html/body/div")
html = element.get_attribute("innerHTML")

# Copy using klembord to keep the formatting
# element.text is plain text and text is html
klembord.set_with_rich_text(element.text, html)

driver.quit()

 

κ²°κ³Ό

 

 

좜처: 

https://stackoverflow.com/questions/75592229/how-to-copy-text-with-formatting-using-python-selenium

 

How to copy text with formatting using Python + Selenium?

I'm using Python and Selenium and I need to copy text from a webpage to the OS Windows clipboard with formatting. For example, when you copy text from a webpage by pressing the Ctrl+C key combinati...

stackoverflow.com

 

 

saveClipboard.py - ν΄λ¦½λ³΄λ“œ κ°€μ Έμ™€μ„œ 파일둜 μ €μž₯ν•˜κΈ°
import klembord

f = open('clipboard1.txt', 'w', encoding='utf-8') # ν…μŠ€νŠΈλ§Œ κ°€μ Έμ˜€κΈ°
f.write( klembord.get_with_rich_text()[0] )
f.close()

f = open('clipboard2.txt', 'w', encoding='utf-8') # ν…μŠ€νŠΈ ν˜•μ‹ κ°€μ Έμ˜€κΈ°
f.write( klembord.get_with_rich_text()[1] )
f.close()

 

728x90
λ°˜μ‘ν˜•