import time
from selenium import webdriver
from PIL import Image
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
def getScreenshot():
opt = webdriver.ChromeOptions()
driver = webdriver.Chrome(options=opt)
driver.maximize_window()
driver.get('http://msg.urbtix.hk/')
WebDriverWait(driver,10,0.5).until(EC.visibility_of_element_located((By.LINK_TEXT,'登入')))
driver.find_element_by_class_name('mem-login-state-link').click()
WebDriverWait(driver,10,0.5).until(EC.presence_of_element_located((By.ID,'captchaImage')))
time.sleep(2)
currentpage = './captchaImages/currentpage.png'
driver.save_screenshot(currentpage)
captchaImage_element = driver.find_element_by_id('captchaImage')
left = captchaImage_element.location['x']
top = captchaImage_element.location['y']
right = captchaImage_element.location['x'] + captchaImage_element.size['width']
bottom = captchaImage_element.location['y'] + captchaImage_element.size['height']
image = Image.open(currentpage)
image = image.crop((left,top,right,bottom))
image.save('./images/capimg.png')
click_captchaImage1 = driver.find_elements_by_xpath('//form/div/div/table/tbody/tr/td/table/tbody/tr[@class="login-tbl-captcha-image-row captcha-row"][3]/td/table/tbody/tr/td/img')
for i,img in enumerate(click_captchaImage1):
left =img.location['x']
top =img.location['y']
right =img.location['x'] + img.size['width']
bottom =img.location['y'] + img.size['height']
img = Image.open(currentpage)
img =img.crop((left,top,right,bottom))
img.save('./images/' + str(i) + '.png')
if __name__ == '__main__':
getScreenshot()