'Chrome driver didn't show characters correctly
I'm trying to take screenshot using selenium webdriver, but webdriver can't show traditional-chinese correctly. the result is like this.
here's my code
from selenium.webdriver.common.by import By
import cv2
import time
options = webdriver.ChromeOptions()
options.add_argument('--headless') #無頭啟動,無視窗載入
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome(executable_path = "/usr/bin/chromedriver", chrome_options = options)
driver.get("https://swelleye.com/surf-spots/wanli/")
time.sleep(3)
element = driver.find_element(By.CLASS_NAME, "zh")
driver.set_window_size(element.size["width"], element.size["height"])
driver.save_screenshot("test7.png")
img = cv2.imread("test7.png")
crop_img=img[100:100+1000,100:100+2000]
cv2.imwrite("test7.png",crop_img)```
On installing chrome and webdriver, I followed this instruction
https://www.gushiciku.cn/pl/g8zs/zh-tw
Thanks:)
Solution 1:[1]
you should download font like this
!apt-get install fonts-indic
!apt-get install fonts-noto
!apt-get install fonts-noto-cjk
it is because of options.add_argument('--headless') #??????????
full code:(in colab)
!pip install selenium
!apt-get update # to update ubuntu to correctly run apt install
!apt install chromium-chromedriver
!cp /usr/lib/chromium-browser/chromedriver /usr/bin
!pip install beautifulsoup4
!pip install opencv-python
!apt-get install fonts-indic
!apt-get install fonts-noto
!apt-get install fonts-noto-cjk
import sys
sys.path.insert(0,'/usr/lib/chromium-browser/chromedriver')
from selenium import webdriver
import bs4 as soup
import cv2
from google.colab.patches import cv2_imshow # colab show image
baseURL="https://XXXXX/"
prefs = {
"translate_whitelists": {"your native language":"zh"},
"translate":{"enabled":"True"}
}
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_experimental_option('prefs', prefs)
chrome_options.add_argument('--disable-dev-shm-usage')
wd = webdriver.Chrome('chromedriver',chrome_options=chrome_options)
wd.get(baseURL)
wd.get_screenshot_as_file("home.png")
img = cv2.imread("/content/home.png")
cv2_imshow(img)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |