Esempio n. 1
0
def irctc_login():
    browser_name = input("Enter browser name firefox/chrome : ")
    user_id = input("Enter irctc user_id : ")
    password = input("Enter irctc password : "******"value")

                # Check if captcha type is traditional
                if captcha_type == 'traditional':
                    captcha_image = WebDriverWait(driver, 10).until(
                        ec.presence_of_element_located(
                            (By.CSS_SELECTOR, 'img#nlpCaptchaImg')))

                # Check if Banner Captcha is present
                else:
                    captcha_image = WebDriverWait(driver, 10).until(
                        ec.presence_of_element_located(
                            (By.CSS_SELECTOR, 'img#captchaImg')))
                image_url = captcha_image.get_attribute("src")

                # Convert theme captcha into banner captcha
                if image_url.split('/')[-1] == 'theme1':
                    image_url = image_url.replace('theme1', 'banner')
                download_image(image_url)

            except TimeoutException:
                captcha_image = WebDriverWait(driver, 5).until(
                    ec.presence_of_element_located(
                        (By.CSS_SELECTOR, 'img#cimage')))

                # found basic captcha
                banner_captcha_flag = False

                # Incase of basic captcha, img download is not possible because every time we hit image url it produce
                # a fresh captcha
                # So instead we take screenshot of <img> tag.

                if driver.name.lower() == 'firefox':
                    captcha_image.screenshot(str(src_path.joinpath('0.png')))

                else:
                    # for chrome, take screenshot of full webpage and crop the part in which we are interested

                    location = captcha_image.location
                    size = captcha_image.size

                    driver.save_screenshot(str(src_path.joinpath('0.png')))

                    x = location['x']
                    y = location['y']
                    width = location['x'] + size['width']
                    height = location['y'] + size['height']

                    captcha_image = Image.open(str(src_path.joinpath('0.png')))
                    captcha_image = captcha_image.crop(
                        (int(x), int(y), int(width), int(height)))
                    captcha_image.save(str(src_path.joinpath('0.png')),
                                       optimize=True,
                                       quality=95)

            # get text from captcha image
            img_text = do_ocr(src_path.joinpath('0.png'), browser_name)
            if banner_captcha_flag:
                img_text = img_text.split(':')[-1].replace(' ',
                                                           '').strip().upper()
            else:
                img_text = img_text.strip().upper()

            # switch back to main window
            driver.switch_to.window(main_window)

            driver.implicitly_wait(5)

            # type text in captcha text field
            if banner_captcha_flag:
                captcha_textfield = WebDriverWait(driver, 10).until(
                    ec.presence_of_element_located(
                        (By.CSS_SELECTOR, '#nlpAnswer')))
                captcha_textfield.send_keys(img_text)
            else:
                captcha_textfield = WebDriverWait(driver, 10).until(
                    ec.presence_of_element_located(
                        (By.CSS_SELECTOR, 'input.loginCaptcha')))
                captcha_textfield.send_keys(img_text)

            # Click on login button
            login_button = WebDriverWait(driver, 10).until(
                ec.presence_of_element_located(
                    (By.CSS_SELECTOR, '#loginbutton')))
            login_button.click()

            try:
                # Handle 'wrong captcha' pop up window
                WebDriverWait(driver, 10).until(
                    ec.presence_of_element_located(
                        (By.CSS_SELECTOR, '#loginerrorpanelok')))
                invalid_captcha_button = WebDriverWait(driver, 10).until(

                    # Once I got an error where this particular button was not interactable though it was visible in dom
                    # To handle that below wait was added
                    ec.element_to_be_clickable(
                        (By.CSS_SELECTOR, '#loginerrorpanelok')))
                invalid_captcha_button.click()

            except TimeoutException:
                print('Login Successful')
                break

    except Exception as ex:
        driver.save_screenshot(
            str(
                src_path.parent.joinpath('exception_screenshot').joinpath(
                    'exception1.png')))
        print(ex.message)

    finally:
        driver.quit()
        print('Script Complete')