-
Notifications
You must be signed in to change notification settings - Fork 6
/
qrcode.py
41 lines (31 loc) · 1.45 KB
/
qrcode.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import base64
import contextlib
from PIL import Image
from io import BytesIO
from selenium.common.exceptions import WebDriverException
from utils import js_get_element_by_xpath, wait_for
class Qrcode:
def __init__(self, driver):
self.__driver = driver
print("Loading QRCODE")
with contextlib.suppress(WebDriverException):
self.__get_qrcode_canvas()
return
with contextlib.suppress(WebDriverException):
self.__get_qrcode_img()
return
raise Exception("Error in get QRCODE")
def __get_qrcode_img(self):
qrcode = wait_for(lambda: self.__driver.find_element_by_xpath("/html/body/div[1]/div/div/div[2]/div[1]/div/div[2]/div/img"))
qrcode_src = qrcode.get_attribute("src")
qrcode_src = qrcode_src.replace("data:image/png;base64,", "")
qrcode_image = Image.open(BytesIO(base64.b64decode(qrcode_src)))
qrcode_image.save("qrcode.png", "PNG")
qrcode_image.show()
def __get_qrcode_canvas(self):
canvas_64 = "return %s.toDataURL('image/png')" % js_get_element_by_xpath("/html/body/div[1]/div/div/div[2]/div[1]/div/div[2]/div/canvas")
qrcode_src = wait_for(lambda: self.__driver.execute_script(canvas_64))
qrcode_src = qrcode_src.replace("data:image/png;base64,", "")
qrcode_image = Image.open(BytesIO(base64.b64decode(qrcode_src)))
qrcode_image.save("qrcode.png", "PNG")
qrcode_image.show()