def handle_captcha(self): # wait for captcha to load time.sleep(DEFAULT_MAX_WEIRD_PAGE_DELAY) try: if self.driver.find_element_by_xpath( '//form[@action="/errors/validateCaptcha"]'): try: log.info("Stuck on a captcha... Lets try to solve it.") captcha = AmazonCaptcha.fromdriver(self.driver) solution = captcha.solve() log.info(f"The solution is: {solution}") if solution == "Not solved": log.info( f"Failed to solve {captcha.image_link}, lets reload and get a new captcha." ) self.driver.refresh() else: if self.no_screenshots: self.notification_handler.send_notification( "Solving captcha") else: self.save_screenshot("captcha") self.driver.find_element_by_xpath( '//*[@id="captchacharacters"]').send_keys( solution + Keys.RETURN) except Exception as e: log.debug(e) log.info( "Error trying to solve captcha. Refresh and retry.") self.driver.refresh() except exceptions.NoSuchElementException: log.error("captcha page does not contain captcha element") log.error("refreshing") self.driver.refresh()
def get_captcha_help(self): if not self.on_captcha_page(): log.info("Not on captcha page.") return try: log.info("Stuck on a captcha... Lets try to solve it.") captcha = AmazonCaptcha.fromdriver(self.driver) solution = captcha.solve() log.info(f"The solution is: {solution}") if solution == "Not solved": log.info( f"Failed to solve {captcha.image_link}, lets reload and get a new captcha." ) self.driver.refresh() time.sleep(5) self.get_captcha_help() else: self.save_screenshot("captcha") self.driver.find_element_by_xpath( '//*[@id="captchacharacters"]').send_keys(solution + Keys.RETURN) except Exception as e: log.debug(e) log.info("Error trying to solve captcha. Refresh and retry.") self.driver.refresh() time.sleep(5)
def validate_captcha(chromeDriver): time.sleep(1) l.info("Solving CAPTCHA") chromeDriver.get('https://www.amazon.com/errors/validateCaptcha') captcha = AmazonCaptcha.fromdriver(chromeDriver) solution = captcha.solve() chromeDriver.find_element_by_id('captchacharacters').send_keys(solution) chromeDriver.find_element_by_class_name('a-button-text').click() time.sleep(1)
def solveCaptcha(d: webdriver.Chrome): if 'Amazon.com' == d.title: try: time.sleep(random.randint(1,3)) captcha = AmazonCaptcha.fromdriver(d).solve() print(captcha + 'doc: ' + d.page_source) d.find_element_by_id('captchacharacters').send_keys(captcha) d.find_element_by_tag_name('button').click() except: l('Unable to pass captcha!!!')
def test_fromdriver(self): capabilities = webdriver.ChromeCapabilities() capabilities.add_argument('--headless') capabilities.add_argument('--no-sandbox') driver = webdriver.ChromeDriver(ChromeDriverManager().install(), desired_capabilities = capabilities.desired) solutions = list() for i in range(5): driver.get('https://www.amazon.com/errors/validateCaptcha') captcha = AmazonCaptcha.fromdriver(driver) solutions.append(len(captcha.solve())) driver.quit() self.assertIn(6, solutions)
def login(self): log.info("Email") email_field = None password_field = None timeout = self.get_timeout() while True: try: email_field = self.driver.find_element_by_xpath('//*[@id="ap_email"]') break except exceptions.NoSuchElementException: try: password_field = self.driver.find_element_by_xpath( '//*[@id="ap_password"]' ) break except exceptions.NoSuchElementException: pass if time.time() > timeout: break if email_field: try: email_field.send_keys(self.username + Keys.RETURN) except exceptions.ElementNotInteractableException: log.info("Email not needed.") else: log.info("Email not needed.") if self.driver.find_elements_by_xpath('//*[@id="auth-error-message-box"]'): log.error("Login failed, delete your credentials file") time.sleep(240) exit(1) time.sleep(self.page_wait_delay()) log.info("Remember me checkbox") try: self.driver.find_element_by_xpath('//*[@name="rememberMe"]').click() except exceptions.NoSuchElementException: log.error("Remember me checkbox did not exist") log.info("Password") password_field = None timeout = self.get_timeout() current_page = self.driver.title while True: try: password_field = self.driver.find_element_by_xpath( '//*[@id="ap_password"]' ) break except exceptions.NoSuchElementException: pass if time.time() > timeout: break if password_field: password_field.send_keys(self.password + Keys.RETURN) self.wait_for_page_change(current_page) else: log.error("Password entry box did not exist") # check for captcha try: if self.driver.find_element_by_xpath( '//form[@action="/errors/validateCaptcha"]' ): try: log.info("Stuck on a captcha... Lets try to solve it.") captcha = AmazonCaptcha.fromdriver(self.driver) solution = captcha.solve() log.info(f"The solution is: {solution}") if solution == "Not solved": log.info( f"Failed to solve {captcha.image_link}, lets reload and get a new captcha." ) self.driver.refresh() else: self.send_notification( "Solving catpcha", "captcha", self.take_screenshots ) self.driver.find_element_by_xpath( '//*[@id="captchacharacters"]' ).send_keys(solution + Keys.RETURN) except Exception as e: log.debug(e) log.info("Error trying to solve captcha. Refresh and retry.") self.driver.refresh() except exceptions.NoSuchElementException: log.debug("login page did not have captcha element") # time.sleep(self.page_wait_delay()) if self.driver.title in TWOFA_TITLES: log.info("enter in your two-step verification code in browser") while self.driver.title in TWOFA_TITLES: time.sleep(0.2) log.info(f"Logged in as {self.username}")