def scroll_up(self): sc_height = self.driver.execute_script("return window.pageYOffset") while sc_height > 0: with Listener(on_press=on_press, on_release=on_release) as listener: time.sleep(0.03) if not listener.running: break if similar("stop", voice.command) and voice.executed is False: voice.executed = True self.scroll_speed = self.scroll_base_speed print(voice.command) return elif similar("go faster", voice.command) and voice.executed is False: voice.executed = True self.scroll_speed += self.scroll_update_speed print(voice.command, self.scroll_speed) elif similar("slow down", voice.command) and voice.executed is False: voice.executed = True if self.scroll_speed > self.scroll_base_speed: self.scroll_speed -= self.scroll_update_speed print(voice.command, self.scroll_speed) self.driver.execute_script("window.scrollTo(0, %s)" % sc_height) sc_height -= self.scroll_speed self.scroll_speed = self.scroll_base_speed
def hover(self, value): try: buttons = self.driver.find_elements_by_css_selector("button") + \ self.driver.find_elements_by_css_selector("a") avg_value = None for button in buttons: if similar(value, button.text) is True: avg_value = button.text break if avg_value is None: for button in buttons: if value.lower() in button.text.lower(): avg_value = button.text break if avg_value is None: raise Exception elem = self.driver.find_element_by_link_text(avg_value) action = ActionChains(self.driver).move_to_element(elem) action.perform() except: print("Couldn't hover over " + value)
def click(self, value): try: nr_tabs = len(self.driver.window_handles) buttons = self.driver.find_elements_by_css_selector("button") + \ self.driver.find_elements_by_css_selector("a") avg_value = None for button in buttons: if similar(value, button.text) is True: avg_value = button.text break if avg_value is None: for button in buttons: if value.lower() in button.text.lower(): avg_value = button.text break if avg_value is None: raise Exception self.driver.find_element_by_link_text(avg_value).click() if len(self.driver.window_handles) != nr_tabs: self.switch_tab() except: print("Couldn't find button " + value)
# browser.switch_tab() # browser.close_tab() # browser.back() # thread = Thread(target=voice.listen) # thread.daemon = True # thread.start() while True: listener = Listener(on_press=on_press, on_release=on_release) listener.start() listener.join() if voice.executed is False: voice.executed = True print(voice.command) if similar("go to", " ".join(voice.command.split()[:2])): url = " ".join(voice.command.split()[2:]) browser.open(url) elif similar("search", " ".join(voice.command.split()[:1])): text = " ".join(voice.command.split()[1:]) browser.search(text) elif similar("click on", " ".join(voice.command.split()[:2])): value = " ".join(voice.command.split()[2:]) browser.click(value) elif similar("hover on", " ".join(voice.command.split()[:2])): value = " ".join(voice.command.split()[2:]) browser.hover(value)