๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
  • Tried. Failed. Logged.
๐Ÿ‘จ๐Ÿผ‍๐Ÿ’ป๊ฐœ๋ฐœ/์…€๋ ˆ๋‹ˆ์›€

์…€๋ ˆ๋‹ˆ์›€ - 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