def getCoursesList(db,cur,academy,special,term): webdriver.switch_to.frame('frameCourseView') tbody=webdriver.find_element_by_tag_name('tbody') tbody=webdriver.find_element_by_tag_name('tbody') trOut=tbody.find_element_by_tag_name('tr') courses=trOut.find_elements_by_tag_name('tr') if len(courses)>1: i=1 while i<len(courses): Id,name,point,time,area,teacher=coursesDetails(courses[i]) i=i+1 if Id == None: continue if len(teacher)>=148: teacher=teacherPartial(teacher) print Id,name,point,time,area,teacher if checkTheSameCourse(cur,Id,teacher): continue try: sql="INSERT INTO CoursesMain(id,name,point,time,area,teacher,academy,special,term) VALUES('%s','%s',%.1f,%d,'%s','%s','%s','%s','%s')" % (Id,name,float(point),int(time),area,teacher,academy,special,term) result=cur.execute(sql) if result == 1: db.commit() except Exception, e: db.rollback() raise e
def scroll_to_top(webdriver): """ Scrolls to the top of the window. Useful when elements are hidden :param webdriver: The Selenium webdriver """ webdriver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.HOME) time.sleep(10) # Have to sleep to allow dominos server to catch up
def capture(): seleniumlib = BuiltIn().get_library_instance('SeleniumLibrary') webdriver = seleniumlib.driver S = lambda X: webdriver.execute_script('return document.body.parentNode.scroll' + X) webdriver.set_window_size(S('Width'), S('Height')) # May need manual adjustment webdriver.find_element_by_tag_name('body').screenshot('image_name.png') # capturing body tag gives full page screenshot #body_tag = webdriver.find_element_by_tag_name('body') #body_tag.screenshot('image_name.png')
def get_picture_links(webdriver, total_posts): '''This function will find all links that have pictures on the page. Note that there are only about 12 photos per page so the program will 'scroll' as many times as necessary to get all photos. Parameters: the active webdriver, the total posts for that user Returns: a set of picture and video links ''' posts = total_posts.split(' ')[0].replace(',', '') num_posts = int(posts) pages = (num_posts//12) #This will determine how many times we scroll on a page link_set = set() html = webdriver.find_element_by_tag_name('html') for _ in range(pages): all_links = webdriver.find_elements_by_tag_name('a') for link in all_links: picture_link = link.get_attribute('href') if picture_link.find('/p/') != -1: #only adding links that have are pictures link_set.add(picture_link) html.send_keys(Keys.END) time.sleep(3) return link_set
def login(webdriver): webdriver.get(url) webdriver.implicitly_wait(10) webdriver.switch_to.frame(webdriver.find_element_by_tag_name('iframe')) webdriver.find_element_by_id('username').send_keys(USERNAME) webdriver.find_element_by_id('password').send_keys(PASSWORD) webdriver.find_element_by_xpath("//input[@type='image']").click() webdriver.implicitly_wait(5)
def setup(): webdriver.get( 'https://randomwordgenerator.com/would-you-rather-question.php') sleep(1) qinput = webdriver.find_element_by_tag_name("input") qinput.clear() qinput.send_keys("50") return qinput
def form_data(webdriver): input1 = webdriver.find_element_by_tag_name("input") input1.send_keys("Ivan") input2 = webdriver.find_element_by_name("last_name") input2.send_keys("Petrov") input3 = webdriver.find_element_by_class_name("city") input3.send_keys("Smolensk") input4 = webdriver.find_element_by_id("country") input4.send_keys("Russia") button = webdriver.find_element_by_xpath(xpath) button.click() return
def smart_wait(webdriver, method, element_path): for i in range(10): if i >= 9: print("timeout") break try: if (method == "xpath"): if webdriver.find_element_by_xpath(element_path): break if (method == "class_name"): if webdriver.find_element_by_class_name(element_path): break if (method == "tag_name"): if webdriver.find_element_by_tag_name(element_path): break except: print("wait for find element") time.sleep(1)
def auth_with_data(self, webdriver, epicgames_login, epicgames_pswd): webdriver.get( 'https://www.epicgames.com/id/login?redirectUrl=https%3A%2F%2Fwww.epicgames.com%2Fstore%2Fru%2F&noHostRedirect=true' ) wait_for_element = WebDriverWait(webdriver, TIMEOUT).until( EC.element_to_be_clickable((By.ID, 'usernameOrEmail'))) webdriver.find_element_by_id('usernameOrEmail').click() webdriver.find_element_by_id('usernameOrEmail').send_keys( epicgames_login) webdriver.find_element_by_id('password').send_keys(epicgames_pswd) wait_for_element = WebDriverWait(webdriver, TIMEOUT).until( EC.element_to_be_clickable((By.ID, 'login'))) webdriver.find_element_by_id('login').send_keys(Keys.RETURN) time.sleep(30) if re.findall('(\w+)-(\w+)-(\w+)', webdriver.find_element_by_tag_name('body').text): return 'need_reset' return 'ok'
def find(webdriver, by, css_selector_val): ''' Wrapper function of selenium python to find an elment using locator and locator_value(css_selector_val) Arguments --------- webdriver - object of selenium.webdriver.chrome.webdriver.WebDriver . by - element locator name . contraint: expected value:- id, name, xpath, link_text, partial_link_text, tag_name, class_name, css_selector other value than the expected will return None css_selector_val- value for the element locator i.e. arg 'by' example:- to find all elements with class_name=contact, value for css_selector_val is 'contact' Return --------- Webelement - if the value of arg 'by' is an expected value or None - if the value of arg 'by' is an unexpected value ''' if by == 'id': return webdriver.find_element_by_id(css_selector_val) if by == 'name': return webdriver.find_element_by_name(css_selector_val) if by == 'xpath': return webdriver.find_element_by_xpath(css_selector_val) if by == 'link_text': return webdriver.find_element_by_link_text(css_selector_val) if by == 'partial_link_text': return webdriver.find_element_by_partial_link_text(css_selector_val) if by == 'tag_name': return webdriver.find_element_by_tag_name(css_selector_val) if by == 'class_name': return webdriver.find_element_by_class_name(css_selector_val) if by == 'css_selector': return webdriver.find_element_by_css_selector(css_selector_val) else: return None
driver.back() # 页面后退 driver.forward() # 页面前进 driver.set_window_size(540, 960) # 设置窗口大小,如手机分辨率为540*960 driver.maximize_window() # 最大化窗口 driver.get_screenshot_as_file("d:\\test\\aa.jpg") # 截图,定义文件位置名称 # 获取当前window的截图,出现IOError时候返回False,截图成功返回True。 test.img = driver.get_screenshot_as_base64() driver.close() # 关闭当前窗口 driver.quit() # 关闭所有窗口,退出浏览器进程 # 2.2 -------常用18种元素定位------ # 定位单个元素,8种 webdriver.find_element_by_id("") webdriver.find_element_by_name("") webdriver.find_element_by_class_name("") webdriver.find_element_by_tag_name("") webdriver.find_element_by_link_text("") webdriver.find_element_by_partial_link_text("") webdriver.find_element_by_css_selector("") webdriver.find_element_by_xpath("") # 定位一组元素,返回对象列表 8种 webdriver.find_elements_by_id("") # id复数定位 webdriver.find_elements_by_name("") # name复数定位 webdriver.find_elements_by_class_name("") # class复数定位 webdriver.find_elements_by_tag_name("") # teg复数定位 webdriver.find_elements_by_link_text("") # link复数定位 webdriver.find_elements_by_partial_link_text("") # partial_link复数定位 webdriver.find_elements_by_css_selector("") # css_selector 复数定位 webdriver.find_elements_by_xpath("") # xpath复数定位 # 这两种是参数化的方法,对上面各8种的总结 webdriver.find_element(by='id', value="")
webdriver.get( "https://wrem.sis.yorku.ca/Apps/WebObjects/REM.woa/wa/DirectAction/rem") webdriver.get_screenshot_as_file("addCourse.png") login = webdriver.find_element_by_name("loginform") userElement = login.find_element_by_id("mli") passElement = login.find_element_by_id("password") loginButtonElement = webdriver.find_element_by_name("dologin") #This is where you input your yorku passport username userElement.send_keys("USER") #This iswhere you input your yorku passport password passElement.send_keys("PASS") loginButtonElement.click() webdriver.get_screenshot_as_file("confirmation.png") WebDriverWait(webdriver, 10).until(EC.title_contains("REM - Main")) webdriver.get_screenshot_as_file("enrollmentPage.png") selection = webdriver.find_element_by_tag_name("select") selection.click() allOptions = selection.find_elements_by_tag_name("option") for option in allOptions: print "Option is:" + option.get_attribute("value") # at this time option 1 = enroll in Fall2019/Winter2020 # the options might change over time if (option.get_attribute("value") == '1'): option.click() webdriver.get_screenshot_as_file("optionSelected.png") webdriver.quit()
username = webdriver.find_element_by_name('username') username.send_keys('enter user name') password = webdriver.find_element_by_name('password') passfile = open('mat.txt', 'r') def display(a): print("log in password " + a) for i in passfile: try: password.send_keys(i) submit = webdriver.find_element_by_tag_name('form') submit.submit() c = webdriver.current_url sleep(5) b = webdriver.find_element_by_xpath( "//button[contains(text(), 'Not Now')]") b.click() sleep(5) c = webdriver.current_url except NoSuchElementException: password.send_keys(Keys.CONTROL, "a", Keys.DELETE) sleep(5) url2 = webdriver.current_url if c != url2: display(i) break
def check_exists_by_tag(webdriver, tag): try: webdriver.find_element_by_tag_name(tag) except Exception: return False return True
no_thanks=webdriver.find_element_by_xpath(NO_THANKS_BUTTON_XPATH) no_thanks.click() print("no thanks...") sleep(1.25) VIEW_CART_XPATH="/html/body/div[10]/div[3]/div[2]/div/div/div[2]/div[2]/button[2]" view_cart=webdriver.find_element_by_xpath(VIEW_CART_XPATH) view_cart.click() print("viewing cart...") sleep(0.75) html = webdriver.find_element_by_tag_name('html') html.send_keys(Keys.END) #goes all the way down the page print("going to end...") sleep(2) print("went to end...") sleep(0.5) CHECKOUT_XPATH="/html/body/div[8]/div[1]/section/div/div/form/div[2]/div[3]/div/div/div[3]/div/button" checkout_button=webdriver.find_element_by_xpath(CHECKOUT_XPATH) checkout_button.click() print("checking out...") sleep(1) ''' GUEST_CHECKOUT_XPATH="/html/body/div[5]/div/div[2]/div/div/div[1]/form[2]/div[2]/div/button" guest_checkout=webdriver.find_element_by_xpath(GUEST_CHECKOUT_XPATH) guest_checkout.click() print("guest checkout..")
def _js_switcher(self, webdriver): webdriver.get('Chrome://settings/content/javascript') webdriver.find_element_by_tag_name('body').send_keys(Keys.TAB * 5 + Keys.RETURN)
def _js_disble_chromium(self, webdriver): webdriver.get('Chrome://settings/content/javascript') webdriver.find_element_by_tag_name('body').send_keys( Keys.TAB * 6 + Keys.RETURN + 'passport.yandex.ru' + Keys.RETURN)