def _get_question_transcript(self): try: original_tab = self.driver.current_window_handle self.open_new_tab(CHEGG_QUESTION_BASE_URL + self.current_qid) except Exception as err: print(err) return None try: # Get the transcript transcript = self.driver.find_element_by_class_name( "transcribed-image-text-show").text except NoSuchElementException as err: # At this point, reCaptcha comes quite often if self.is_bot_compromised(): if not self.solve_captcha_automatically(): solve_captcha_manually() generate_random_delay() # Delay try: # Get the transcript transcript = self.driver.find_element_by_class_name( "transcribed-image-text-show").text self.close_recent_tab() # Switch to original tab self.driver.switch_to.window(original_tab) return transcript except Exception as err: print(err) return None
def submit_answer(self): try: self.driver.find_element_by_id("ext-submit-btn").click() while self.driver.find_element_by_id( "countdown").text != "9 min 55 sec": pass except StaleElementReferenceException as err: generate_random_delay() # Delay except Exception as err: print(err) return print("Answer submitted\n")
def search_question(self, text): generate_random_delay() # Delay try: if self.switch_to_tab_with_matching_url( CHEGG_RESULTS_PAGE_BASE_URL): self._search_question_on_results_page(text) else: self.open_new_tab(CHEGG_HOMEPAGE_URL) self._search_question_on_homepage(text) except Exception as err: print(err) return False return True
def _search_question_on_results_page(self, text): try: search_field = self.driver.find_element_by_xpath( '//*[@id="chegg-searchbox"]') search_field.click() self.driver.execute_script( "document.querySelector('#chegg-searchbox').value = ''") generate_random_delay() # Delay search_field.send_keys(text) search_field.send_keys(Keys.RETURN) except Exception as err: print(err) return False return True
def login_to_chegg(self, id, password): try: self.driver.get("https://www.chegg.com") sign_in_btn = self.driver.find_element_by_xpath( '//*[@id="eggshell-15"]/a') generate_random_delay() # Delay sign_in_btn.click() email_field = self.driver.find_element_by_xpath( '//*[@id="emailForSignIn"]') password_field = self.driver.find_element_by_xpath( '//*[@id="passwordForSignIn"]') generate_random_delay() # Delay email_field.send_keys(id) password_field.send_keys(password) password_field.send_keys(Keys.RETURN) except Exception as err: print(err)
def get_question_text(self): generate_random_delay() # Delay try: qid = self._get_qid() if qid != None and qid != self.current_qid: self.current_qid = qid print(f"Question ID: {self.current_qid}") # Check if question has image(s) or not if self._does_question_contain_images(): text = self._get_question_transcript() else: text = self.driver.find_element_by_class_name( "question").get_attribute("innerText") return text except Exception as err: print(err) return None
def solve_captcha_automatically(self): try: iframe = self.driver.find_element_by_xpath( "//iframe[starts-with(@name, 'a-') and starts-with(@src, 'https://www.google.com/recaptcha')]" ) self.driver.switch_to.frame(iframe) self.driver.find_element_by_css_selector( "div.rc-anchor-content").click() # Check if checkbox checkmark is applied or not style = self.driver.find_element_by_class_name( "recaptcha-checkbox-checkmark").get_attribute("style") if style == "null": return False # Proceed to fill captcha manually self.driver.switch_to.default_content() except Exception as err: print(err) return False generate_random_delay() # Delay return True
def _search_question_on_homepage(self, text): try: search_field = self.driver.find_element_by_xpath( '//*[@id="chegg-searchbox"]') except Exception as err: print(err) # At this point, reCaptcha comes quite often if self.is_bot_compromised(): if not self.solve_captcha_automatically(): solve_captcha_manually() try: search_field = self.driver.find_element_by_xpath( '//*[@id="chegg-searchbox"]') search_field.click() generate_random_delay() # Delay search_field.send_keys(text) generate_random_delay() # Delay search_field.send_keys(Keys.RETURN) except Exception as err: print(err) return False return True
def skip_question(self): try: start_time = time() self.driver.find_element_by_id("ext-skip-btn").click() while self.driver.find_element_by_id("countdown").text != "10 min": end_time = time() elapsed_time = end_time - start_time if elapsed_time >= TIMEOUT_TIME: raise TimeoutException except StaleElementReferenceException as err: generate_random_delay() # Delay except TimeoutException as err: # Refresh page self.refresh_current_tab() # Check if bot has been compromised or not if self.is_bot_compromised(): if not self.solve_captcha_automatically(): solve_captcha_manually() except Exception as err: print(err) return print("Question skipped\n")