我们从Python开源项目中,提取了以下5个代码示例,用于说明如何使用selenium.webdriver.common.keys.Keys.COMMAND。
def open_devtools(self): self.driver.find_element_by_tag_name('body').send_keys(Keys.LEFT_ALT + Keys.COMMAND + 'i')
def open_tab(self, url): """ Opens a new tab and switches focus to it. NB: Currently not used because send_keys on the driver doesn't work on OSX for Chrome (at least, might be others). """ orig_handles = self.driver.window_handles if mozinfo.os == "mac": self.driver.find_element_by_tag_name( 'body').send_keys(Keys.COMMAND + "t") else: self.driver.find_element_by_tag_name( 'body').send_keys(Keys.CONTROL + "t") time.sleep(0.25) new_handles = set(self.driver.window_handles) - orig_handles new_handle = list(new_handles)[0] self.driver.switch_to_window(new_handle) self.driver.get(url) # On Fx at least the handle can change after you load content. new_handles = set(self.driver.window_handles) - orig_handles new_handle = list(new_handles)[0] self.tabs.append(new_handle)
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 autologin(ip,port,frst): """ m = requests.Session() #driver = my_proxy('127.0.0.1','8080') driver = webdriver.Firefox() lnk="http://"+ip+":"+port driver.get(lnk) #driver.execute_script('''var link = arguments[0];window.open("link","_blank");''',lnk) logged=driver.execute_script("confirm('ar u logged in with admin/admin?')") if logged == True: frst=False driver.find_element_by_tag_name('body').send_keys(Keys.COMMAND + 't') elif logged == False: if frst == True: frst=False elif frst == False: driver.find_element_by_tag_name('body').send_keys(Keys.COMMAND + 'w') cap=raw_input('enter captcha , type none if no captcha '); if cap != "none": print "ur cap is > " + cap pars = { 'account': 'YWRtaW46YWRtaW4','captcha_code': cap} else: pars = { 'account': 'YWRtaW46YWRtaW4'} """ #resp = m.get("%s" % lnk, data=pars) #,proxies=proxyDict) #print resp #driver = webdriver.PhantomJS() #driver.delete_all_cookies() """key=[] value=[] for i in range(0,len(m.cookies.get_dict().keys())): key.append(str(m.cookies.get_dict().keys()[i])) value.append(str(m.cookies.get_dict()[key[i]])) driver.add_cookie({'name':key[i], 'value':value[i]}) print "key="+key[i]+" "+"value=" + value[i] """ """ hacked=driver.execute_script("return confirm('did the webcam open?')") if hacked == True: driver.find_element_by_tag_name('body').send_keys(Keys.COMMAND + 't') driver.get(lnk) elif hacked == False: driver.find_element_by_tag_name('body').send_keys(Keys.COMMAND + 'w') driver.close() """ return ###########################################################################
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."