我们从Python开源项目中,提取了以下4个代码示例,用于说明如何使用selenium.webdriver.common.keys.Keys.SHIFT。
def hacker_typing(browser, spinter_selection, code): """We need to break Splinter abstraction and fall back to raw Selenium here. Note: There is a bug of entering parenthesis due to IPython capturing keyboard input. http://stackoverflow.com/questions/22168651/how-to-enter-left-parentheses-into-a-text-box """ elem = spinter_selection[0]._element driver = browser.driver # Activate IPython input mode ActionChains(driver).click(elem).send_keys(Keys.ENTER).perform() # Type in the code a = ActionChains(driver) a.send_keys(code) a.perform() time.sleep(1.0) # Execute the text we just typed a = ActionChains(driver) a.key_down(Keys.SHIFT).send_keys(Keys.ENTER).key_up(Keys.SHIFT) a.perform()
def click(self, *modifiers): """ Clicks the element, optionally while pressing the given modifier keys. Note that support for holding a modifier key is currently experimental, and may not work at all. :param modifiers: modifier keys to press while clicking :Example: Click an element browser.element(name='new_user_button').click() :Example: Click an element with shift key pressed from selenium.webdriver.common.keys import Keys browser.element(name='new_user_button').click(Keys.SHIFT) :Example: Click an element with several modifier keys pressed from selenium.webdriver.common.keys import Keys browser.element(name='new_user_button').click(Keys.SHIFT, Keys.CONTROL) """ def method(): if modifiers: action = ActionChains(self.driver) for mod in modifiers: action.key_down(mod) action.click(self.el) for mod in modifiers: action.key_up(mod) action.perform() else: self.el.click() self._element_call(method, self.wait_for_enabled) self.browser.after_hooks.run()
def open_urls(self, urls, tab_limit=MAX_TABS): """ This works at least on Chrome across platforms. """ # First setup a document with the target URLs. link_doc = 'data:text/html,' link_doc += '<html><head><title>Links!</title></head><body>' id = 0 for url in urls: link_doc += '<a id="%d" href="%s">%d: %s</a><br>' % ( id, url, id, url) id += 1 link_doc += '</body><html>' self.driver.get(link_doc) # Now open each document in a new tab by ctrl+shift clicking the # anchor. for tag in self.driver.find_elements_by_tag_name("a"): action = ActionChains(self.driver) if mozinfo.os == "mac": ctrl_key = Keys.COMMAND else: ctrl_key = Keys.CONTROL action.key_down(ctrl_key).key_down(Keys.SHIFT).click( tag).key_up(Keys.SHIFT).key_up(ctrl_key).perform() time.sleep(self.per_tab_pause) time.sleep(self.settle_wait_time) self.stats.print_stats()
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."