我们从Python开源项目中,提取了以下17个代码示例,用于说明如何使用selenium.webdriver.common.keys.Keys.TAB。
def login_user(browser, username, password): """Logins the user with the given username and password""" browser.get('https://twitter.com/login') #Enter username and password and logs the user in userContainer = browser.find_element_by_xpath('//input[contains(@class, "js-username-field email-input js-initial-focus")]') passwordContainer = browser.find_element_by_xpath('//input[@type = "password"]') action = ActionChains(browser).move_to_element(userContainer).click().send_keys(username) \ .send_keys(Keys.TAB).send_keys(password).perform() submitButton = browser.find_element_by_xpath('//button[@type = "submit"]') action = ActionChains(browser).move_to_element(submitButton).click().perform() sleep(2) #Check if user is logged-in (If there's two 'nav' elements) prof = browser.find_elements_by_xpath('//div[@class = "DashboardProfileCard-content"]') if len(prof) > 0: return True else: return False
def _map_ascii_key_code_to_key(self, key_code): map = { 0: Keys.NULL, 8: Keys.BACK_SPACE, 9: Keys.TAB, 10: Keys.RETURN, 13: Keys.ENTER, 24: Keys.CANCEL, 27: Keys.ESCAPE, 32: Keys.SPACE, 42: Keys.MULTIPLY, 43: Keys.ADD, 44: Keys.SEPARATOR, 45: Keys.SUBTRACT, 56: Keys.DECIMAL, 57: Keys.DIVIDE, 59: Keys.SEMICOLON, 61: Keys.EQUALS, 127: Keys.DELETE } key = map.get(key_code) if key is None: key = chr(key_code) return key
def Transfer_Clicks(browser): try: browser.execute_script("window.scrollBy(0,document.body.scrollHeight)", "") # ???????Javascript?????????????? # browser.execute_script("window.scrollBy(0,5)", "") # ????200????????????? # ActionChains(browser).move_by_offset(0,-80).perform() # ??????80?????????? # ActionChains(browser).click().perform() # ?????? # ActionChains(browser).key_down(Keys.TAB).perform() # ??tab???? # ActionChains(browser).send_keys(Keys.ENTER).perform() # ????ENTER? except: pass return "Transfer successfully \n"
def tab(self, by, selector, message = None): self.log("Tab to next active element. WARNING: Does not work for Safari driver") element = self.driver.switch_to.active_element element.send_keys(Keys.TAB) # script = """ # var f = jQuery('#{0}'), e = f, # hasTag = function(e, tag) {{ return (e.prop("tagName") === tag); }}, # getAttr = function(e, attr) {{ return e.attr(attr); }}, # i = 0; # while (e && (i < 1000)) {{ # e = e.next() # if ( # (getAttr(e, 'tabindex') && (getAttr(e, 'tabindex') != -1)) || # (hasTag(e, 'a') && getAttr(e, 'h')) || # (hasTag(e, 'input') && (getAttr(e, 'type') !== 'hidden')) || # (hasTag(e, 'textarea')) # ) # {{ # f.blur(); # e.focus(); # e = undefined; # }} # i = i + 1; # }} # """.format(selector) # self.log("About to execute '{0}'".format(script)) # self.driver.execute_script(script) # self.log("Executed".format(script))
def press_tab(self): """Simulates the user pressing the tab key on element that is currently focussed. .. versionchanged: 4.0 Changed to operate on the currently focussed element. """ el = self.web_driver.switch_to.active_element el.send_keys(Keys.TAB) # To ensure the element gets blur event which for some reason does not always happen when pressing TAB: self.web_driver.execute_script('if ( "undefined" !== typeof jQuery) {jQuery(arguments[0]).blur();};', el)
def switch_to_new_tab(self, check=True): """Step to switch to new tab.""" main_page = self.app.page_images main_page.webdriver.find_element_by_tag_name('body').send_keys( Keys.CONTROL + Keys.TAB) page = main_page.webdriver.current_window_handle main_page.webdriver.switch_to_window(page)
def final_deals(last_page): """ Finish the last step to ICO. 1. Input the num which we want to invest to the ICO. 2. Get the question that need we to solve. 3. Handle the request and finish the ICO. :return: """ new_mark_page = driver.window_handles driver.switch_to_window(new_mark_page[1]) driver.get(last_page) time.sleep(0.5) driver.find_element_by_class_name("numeric decimal optional nprice-input form-control").clear() driver.find_element_by_class_name("numeric decimal optional nprice-input form-control").sendkey(input("Please input the num" "that you want to invest" "the ICO:")) time.sleep(0.5) driver.find_element_by_class_name("numeric decimal optional nprice-input form-control").send_keys(Keys.TAB) handle_question = driver.find_element_by_class_name("title math-title").text print(handle_question) answer = input("Please input the answer:") driver.find_element_by_class_name("form-control").clear() driver.find_element_by_class_name("form-control").send_keys(answer) time.sleep(0.5) driver.find_element_by_class_name("btn btn-theme submit-btn disabled").click() time.sleep(0.5)
def set_value(self, selector, value, clear=True, blur=True, **kwargs): ''' Sets value of an element by CSS selector. Parameters ---------- selector: str A CSS selector to search for. This can be any valid CSS selector. value: str The value to set on the element matched by the selector. clear: bool Whether or not we should clear the element's value first. If false, value will be appended to the current value of the element. blur: bool Whether or not we should blur the element after setting the value. Defaults to True kwargs: passed on to wait_for_visible ''' typing = kwargs.get('typing', False) typing_speed = kwargs.get('typing_speed', 3) typing_max_delay = kwargs.get('typing_max_delay', .33) self.wait_for_visible(selector, **kwargs) elem = kwargs.get('elem') if not elem: elem = self.get_element(selector) if elem.tag_name == 'select': self.set_select_by_value(elem, value) else: if clear: self.clear(selector) if typing: for k in value: delay = random() / typing_speed if delay > typing_max_delay: delay = typing_max_delay sleep(delay) elem.send_keys(k) else: elem.send_keys(value) if self.driver == 'Gecko': # Thank you so much Mozilla. This is awesome to have to do. self.js("arguments[0].setAttribute('value', '" + value + "')", elem) if blur: elem.send_keys(Keys.TAB)
def set_selectize(self, selector, value, text=None, clear=True, blur=False): ''' Sets visible value of a selectize control based on the "selectized" element. Parameters ---------- selector: str A CSS selector to search for. This can be any valid CSS selector. value: str The value of the option to select. (Stored Value) text: str The visible value that the user sees. (Visible value, if different than the stored value) clear: bool Whether or not we should clear the selectize value first. Defaults to True blur: bool Whether or not we should blur the element after setting the value. This corresponds to the 'selectOnTab' selecize setting. Defaults to False ''' selectize_control = selector + ' + .selectize-control' selectize_input = selectize_control + ' input' # Make sure the selectize control is active so the input is visible self.click(selectize_control) input_element = self.get_element(selectize_input) if clear: input_element.send_keys(Keys.BACK_SPACE) input_element.send_keys(text or value) # Wait for options to be rendered self.wait_for_visible(selectize_control + ' .has-options') if blur: input_element.send_keys(Keys.TAB) else: # Click the option for the given value self.click(selectize_control + ' .option[data-value="{}"]'.format(value))
def switch_tab(self, browser_instance=None, tab_number=None, browser_type="firefox"): """Switching to different tabs in a browser with unique tab_number""" status = True if browser_instance is None: browser_instance = self.current_browser if tab_number is not None: try: tab_number = int(tab_number) except: print_error("{0} is not a valid tab number".format(tab_number)) status = False else: if tab_number > len(browser_instance.window_handles) or tab_number < 1: print_error("{0} is not a valid tab number".format(tab_number)) status = False else: tab_number -= 1 current_tab = 0 current_window_handle = browser_instance.current_window_handle for i in range(0, len(browser_instance.window_handles)): if browser_instance.window_handles[i] == current_window_handle: current_tab = i break if tab_number != current_tab: if current_tab < tab_number: times = tab_number - current_tab else: times = len(browser_instance.window_handles) - current_tab times += tab_number if browser_type == "firefox": action_chains = ActionChains(browser_instance) action_chains.key_down(Keys.ALT) for i in range(0, times): action_chains.send_keys('`') action_chains.perform() else: element = browser_instance.find_element_by_tag_name('body') for i in range(0, times): element.send_keys(Keys.LEFT_CONTROL, Keys.TAB) browser_instance.switch_to.window(browser_instance.window_handles[tab_number]) else: current_tab = 0 current_window_handle = browser_instance.current_window_handle for i in range(0, len(browser_instance.window_handles)): if browser_instance.window_handles[i] == current_window_handle: current_tab = i tab_number = current_tab + 1 if tab_number >= len(browser_instance.window_handles): tab_number = 0 if browser_type == "firefox": browser_instance.find_element_by_tag_name('body').send_keys(Keys.LEFT_ALT, '`') else: browser_instance.find_element_by_tag_name('body').send_keys(Keys.LEFT_CONTROL, Keys.TAB) browser_instance.switch_to.window(browser_instance.window_handles[tab_number]) return status
def close_tab(self, browser_instance=None, tab_number=None, browser_type="firefox"): """Closing tabs in a browser with unique tab_number""" if browser_instance is None: browser_instance = self.current_browser if len(browser_instance.window_handles) > 1: prior_current_tab = False current_window_handler = browser_instance.current_window_handle for i in range(0, len(browser_instance.window_handles)): if browser_instance.window_handles[i] == current_window_handler: prior_current_tab = i status = True if tab_number is not None: status = self.switch_tab(browser_instance, tab_number, browser_type) if status: tab_number = int(tab_number) - 1 browser_instance.find_element_by_tag_name('body').send_keys(Keys.LEFT_CONTROL, 'w') sleep(2) if tab_number == len(browser_instance.window_handles): tab_number -= 1 browser_instance.switch_to.window(browser_instance.window_handles[tab_number]) if prior_current_tab == len(browser_instance.window_handles): prior_current_tab -= 1 if prior_current_tab != tab_number: if tab_number < prior_current_tab: times = prior_current_tab - tab_number else: times = len(browser_instance.window_handles) - tab_number times += prior_current_tab if browser_type == "firefox": action_chains = ActionChains(browser_instance) action_chains.key_down(Keys.ALT) for i in range(0, times): action_chains.send_keys('`') action_chains.perform() else: element = browser_instance.find_element_by_tag_name('body') for i in range(0, times): element.send_keys(Keys.LEFT_CONTROL, Keys.TAB) browser_instance.switch_to.window(browser_instance.window_handles[prior_current_tab]) else: if browser_type == "firefox": print_info("The tab_number argument is None. Current window will be closed") else: print_info("The tab_number argument is None. Current tab will be closed") browser_instance.find_element_by_tag_name('body').send_keys(Keys.LEFT_CONTROL, 'w') if prior_current_tab == len(browser_instance.window_handles): prior_current_tab = 0 browser_instance.switch_to.window(browser_instance.window_handles[prior_current_tab]) else: status = self.close_browser(browser_instance) return status
def RunBot(): '''opens web browser''' chromedriver = "/Users/Tj/Downloads/chromedriver" os.environ["webdriver.chrome.driver"] = chromedriver driver = webdriver.Chrome(chromedriver) driver.get(Shoe_url) add_to_cart = driver.find_element_by_name('add-to-cart-button') add_to_cart.click() time.sleep(5) checkout = driver.find_element_by_css_selector('.adi-gradient-blue') checkout.click() first_name_Elem = driver.find_element_by_id('dwfrm_delivery_singleshipping_shippingAddress_addressFields_firstName') first_name_Elem.send_keys(first_name) last_name_Elem = driver.find_element_by_id('dwfrm_delivery_singleshipping_shippingAddress_addressFields_lastName') last_name_Elem.send_keys(last_name) address_Elem = driver.find_element_by_id('dwfrm_delivery_singleshipping_shippingAddress_addressFields_address1') address_Elem.send_keys(street_address) city_Elem = driver.find_element_by_id('dwfrm_delivery_singleshipping_shippingAddress_addressFields_city') city_Elem.send_keys(city) zip_code_Elem = driver.find_element_by_id('dwfrm_delivery_singleshipping_shippingAddress_addressFields_zip') zip_code_Elem.send_keys(zip_code) phone_number_Elem = driver.find_element_by_id('dwfrm_delivery_singleshipping_shippingAddress_addressFields_phone') phone_number_Elem.send_keys(phone_number) email_Elem = driver.find_element_by_id('dwfrm_delivery_singleshipping_shippingAddress_email_emailAddress') email_Elem.send_keys(email) state_dropdown = driver.find_element_by_css_selector('.ffSelectButton') state_dropdown.click() state_dropdown.send_keys(Keys.DOWN * S) state_dropdown.send_keys(Keys.ENTER) pay = driver.find_element_by_css_selector("#dwfrm_delivery_savedelivery") pay.click() time.sleep(3) name_oncard_Elem = driver.find_element_by_id('dwfrm_payment_creditCard_owner') name_oncard_Elem.clear() name_oncard_Elem.send_keys(first_name + ' ' + last_name) card_number_Elem = driver.find_element_by_id('dwfrm_payment_creditCard_number') card_number_Elem.send_keys(card_number) security_code_Elem = driver.find_element_by_id('dwfrm_payment_creditCard_cvn') security_code_Elem.send_keys(security_code) actions = webdriver.ActionChains(driver) month_dropdown = driver.find_element_by_css_selector('.ffSelectButton') month_dropdown.click() month_dropdown.send_keys(Keys.DOWN * M) month_dropdown.send_keys(Keys.ENTER) year_select = driver.find_element_by_css_selector('.year') actions.move_to_element(year_select) actions.click() actions.send_keys(Keys.DOWN * Y) actions.send_keys(Keys.ENTER) actions.perform() security_code_Elem = driver.find_element_by_id('dwfrm_payment_creditCard_cvn') security_code_Elem.send_keys(security_code) security_code_Elem.send_keys(Keys.TAB * 5) time.sleep(3)
def action(action_name): global page_location if action_name == "click": links = get_links() try: if len(links) > 0: for link in links: print link.text links[0].click() except: print ":speak:Can't open the link." elif action_name == "search": text = get_text("Ready") elem = browser.find_element_by_name('q') # Find the search box elem.clear() elem.send_keys('{}'.format(text) + Keys.RETURN) elif action_name == "back": browser.execute_script("window.history.go(-1)") elif action_name == "top": browser.execute_script("window.scrollTo(0, document.body.scrollHeight);") elif action_name == "down": browser.execute_script("window.scrollBy(0, 400);") elif action_name == "new tab": browser.find_element_by_tag_name('body').send_keys(Keys.COMMAND + 't') browser.get("http://www.google.com") browser.switch_to_window(main_window) elif action_name == "close tab": browser.find_element_by_tag_name('body').send_keys(Keys.COMMAND + 'w') browser.switch_to_window(main_window) elif action_name == "switch": browser.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.SHIFT + Keys.TAB) browser.switch_to_window(main_window) elif action_name == "next": try: browser.find_element_by_link_text('Next').click() except: print ":speak:I couldn't find a next link."