def test_blog(self): #test passwords user = "******" pwd = "instructor1a" driver = self.driver driver.maximize_window() driver.get("https://byzantium-travel.herokuapp.com/home/") time.sleep(1) elem = driver.find_element_by_xpath( "/html/body/nav/nav[2]/div/div/ul[1]/li[4]/a").click() time.sleep(5) # Login Test scripts go elem = driver.find_element_by_id("id_username") elem.send_keys(user) elem = driver.find_element_by_id("id_password") elem.send_keys(pwd) elem.send_keys(Keys.RETURN) time.sleep(5) elem = driver.find_element_by_xpath( "/html/body/nav/nav[2]/div/div/ul[1]/li[5]/a").click() time.sleep(5) def tearDown(self): self.driver.close()
def test_frame(driver): driver.get("http://192.168.1.128:8082/xuepl1/frame/main.html")# 打开网页 sleep(2) frame = driver.find_element_by_xpath("/html/frameset/frameset/frame[1]") # 通过xpath定位元素定位到此框架 driver.switch_to.frame(frame) # 切换到frame框架里 sleep(2) driver.find_element_by_link_text('京东').click() # 定位到京东此链接 sleep(2) # 退出当前 iframe #driver.swich_to.parent_frame() # 回到初始页面 driver.switch_to.default_content()# 退出此框架 iframe = driver.find_element_by_xpath("/html/frameset/frameset/frame[2]") # 通过xpath定位元素定位到此框架 sleep(5) driver.switch_to.frame(iframe) # 切换到此框架里 inpu = driver.find_element_by_xpath('//*[@id="key"]') # 通过xpath定位元素定位到此输入框 inpu.clear() # 清空 inpu.send_keys("手机") # 输入手机 sleep(5) # 点击搜索 sousuo = driver.find_element_by_xpath('//*[@id="search"]/div/div[2]/button') #actions = ActionChains(driver) #actions.move_to_element(sousuo).perform() sousuo.click() sleep(5) sousuo_02 = driver.find_element_by_xpath('//*[@id="brand-14026"]/a/img') sousuo_02.click() sleep(10)
def test_02_add_user(self): driver = self.driver # # driver.get("https://online.actitime.com/ssarangmath/login.do") # # lgn = LoginPage(driver) # lgn.enter_username("*****@*****.**") # lgn.enter_pwd("Shar@8971") # lgn.click_login() # driver.implicitly_wait(10) # time.sleep(3) # # # Verify successful login # pageTitle = self.driver.title # self.assertEqual("actiTIME - Enter Time-Track", pageTitle, "Login not success") driver.find_element_by_xpath( "//*[@id='topnav']/tbody/tr/td[5]/a").click() driver.implicitly_wait(20) driver.find_element_by_xpath( "//*[@id='pageBody']/tbody/tr[1]/td[3]/div/div[3]").click() driver.find_element_by_id("createUserPanel_firstNameField").clear() driver.find_element_by_id("createUserPanel_firstNameField").send_keys( "Sharath") driver.find_element_by_id("createUserPanel_lastNameField").clear() driver.find_element_by_id("createUserPanel_lastNameField").send_keys( "Sarangmath") driver.find_element_by_id("createUserPanel_emailField").clear() driver.find_element_by_id("createUserPanel_emailField").send_keys( "createUserPanel_emailField")
def test_firstCase(): path = "C:\\selenium\\chromedriver.exe" driver = Chrome(executable_path=path) driver.get("http://www.theTestingWorld.com/testings") # maximizing window # driver.maximize_window() # # enter text in input field driver.find_element_by_name("fld_username").send_keys("hello") driver.find_element_by_name("fld_email").send_keys("*****@*****.**") driver.find_element_by_name("fld_password").send_keys("Abc123") driver.find_element_by_name("fld_cpassword").send_keys("Abc123") driver.find_element_by_name("fld_username").clear() driver.find_element_by_name("fld_username").send_keys("hello") driver.find_element_by_name("dob").send_keys("") # Working on Radio button driver.find_element_by_xpath("//input[@value='office']").click() # checkbox click driver.find_element_by_name("terms").click() # Seleck dropdoen obj = Select(driver.find_element_by_name("sex")) # select using visible text obj.select_by_visible_text("Female") # select using index number # obj.select_by_index(1) # select using value string # obj.select_by_value("2") # click button driver.find_element_by_xpath("//input[@type='submit']").click()
def window_alert(driver): #判断是否有评价弹窗 try: driver.find_element_by_xpath( 'd(resourceId="com.mildom.android:id/iv_rate_close")') driver.find_element_by_xpath( 'd(resourceId="com.mildom.android:id/iv_rate_close")').click() except: pass
def test_click_on_the_button(self, chrome_drv): time.sleep(3) driver.find_element_by_xpath( '//*[@id="xp__guests__toggle"]').click() # click on button BTN_CLICK = driver.find_element_by_xpath( '//button[@aria-label="Increase number of Children"]') time.sleep(2) actions = ActionChains(driver) actions.double_click(BTN_CLICK).perform() # double click on button
def play(): if (WebDriverWait(driver, 10, 0.5).until(ifPlay())): driver.find_element_by_xpath("//*[@id='bofqi']").click() print("播放数+1") driver.refresh() time.sleep(20) else: print("播放失败") play()
def test_click_on_the_selection_1(self): # click on age selection_1 time.sleep(2) DATA_GROUP_CHILD_AGE = driver.find_element_by_xpath("//select[@aria-label='Child 1 age']") \ .click() # click on age selection CHILD_AGE_SELECTION = driver.find_element_by_xpath( '//select[@aria-label="Child 1 age"]//' 'option[@value="10"]' '[normalize-space()="10 years old"]').click()
def test_wait(driver): driver.get("http://ui.yansl.com/#/loading") bt = driver.find_element_by_xpath("//span[contains(text(),'指令方式')]") bt.click() WebDriverWait(driver,5,0.5).until( EC.presence_of_element_located((By.XPATH,'//tbody/tr[1]/td[2]/div')) ) bg = driver.find_element_by_xpath("//tbody/tr[1]/td[2]/div") print(bg.text) sleep(10)
def test_ayush(self): driver = self.driver driver.get("https://twitter.com/home") username = driver.find_element_by_xpath( "/html/body/div/div/div/div[2]/main/div/div/div[2]/form/div/div[1]/label/div/div[2]/div/input" ) driver.implicitly_wait(10) password = driver.find_element_by_xpath( "/html/body/div/div/div/div[2]/main/div/div/div[2]/form/div/div[2]/label/div/div[2]/div/input" ) driver.implicitly_wait(10) username.send_keys("ayush_tamra") password.send_keys("") driver.implicitly_wait(10) login_button = driver.find_element_by_xpath( "/html/body/div/div/div/div[2]/main/div/div/div[2]/form/div/div[3]/div/div/span/span" ) login_button.click() driver.implicitly_wait(10) search = driver.find_element_by_xpath( "/html/body/div[2]/div/div/div[2]/main/div/div/div/div[2]/div/div[2]/div/div/div/div[1]/div/div/div/form/div[1]/div/div/div[2]/input" ) search.click() # driver.find_element_by_xpath("//input[@value='']").clear() driver.find_element_by_xpath( "/html/body/div[2]/div/div/div[2]/main/div/div/div/div[2]/div/div[2]/div/div/div/div[1]/div/div/div/form/div[1]/div/div/div[2]/input" ).send_keys("#URGENT") driver.find_element_by_xpath( "/html/body/div[2]/div/div/div[2]/main/div/div/div/div[2]/div/div[2]/div/div/div/div[1]/div/div/div/form/div[1]/div/div/div[2]/input" ).send_keys(Keys.ENTER) driver.find_element_by_link_text("Latest").click() driver.find_element_by_id("tweet-text").click() driver.find_element_by_id("tweet-text").click() UserText = driver.find_element_by_xpath( "//div[@id='tweet-text']/span[4]").text
def test_select(driver): driver.get("http://ui.yansl.com/#/select") sleep(2) select = driver.find_element_by_xpath("//*[@id='form']/form/div[3]/div/div/div[2]/input") select.click() sleep(2) option = driver.find_element_by_xpath("(//span[text()='双皮奶'])[last()]") # 设置一个变量名。通过xpath定位元素 actions = ActionChains(driver) # 类的实例化 actions.move_to_element(option).perform() # 鼠标悬浮在元素上面 sleep(2) option.click() # 点击
def test_source(driver): driver.get("http://ui.yansl.com/") source_01 = driver.find_element_by_xpath('//*[@id="app"]/section/section/aside/ul/li[3]/div') source_01.click() sleep(2) source_02 = driver.find_element_by_xpath("//li[contains(text(),'消息提示')]") source_02.click() sleep(5) source = driver.page_source print(source) assert "手工关闭提示" in source sleep(2)
def test_click_on_the_selection_1(self): time.sleep(2) # click on age selection_2 DATA_GROUP_CHILD_AGE_2 = driver.find_element_by_xpath("//select[@aria-label='Child 2 age']") \ .click() # click on age selection CHILD_AGE_SELECTION_1 = driver.find_element_by_xpath( '//select[@aria-label="Child 2 age"]//' 'option[@value="5"]' '[normalize-space()="5 years old"]').click() EMPTY_FIELD = driver.find_element_by_xpath( '//div[@class="xpi__searchbox ' 'js-ds-layout-events-search-form"]').click( ) # click on empty field
def test_checkbox(driver): driver.get("http://ui.yansl.com/#/checkbox") sleep(2) checkbox = driver.find_element_by_xpath("//*[@id='form']/form/div[2]/div/div/label[1]/span[1]") attribute = checkbox.get_attribute("class") # #获取多选框元素里的“CLASS” if not attribute=='el-checkbox__input is-checked': # 如果多选框里不等于el-checkbox__input is-checked checkbox.click() # 则点击 sleep(10) checkbox = driver.find_element_by_xpath("//*[@id='form']/form/div[2]/div/div/label[1]/span[1]") attribute = checkbox.get_attribute("class") if not attribute=='el-checkbox__input is-checked': checkbox.click() sleep(2)
def accept_deny_account_request(user, accept): # Check if is inactive span_od_xpath = "//td[@title='On Demand']//*[local-name() = 'svg']" is_waiting = driver.find_element_by_xpath(span_od_xpath) is_waiting = is_waiting.get_attribute('data-icon') assert is_waiting == "check" # Click on Edit button button_edit_xpath = "//tr/td//*[@data-icon='edit']" selenium_util.wait_to_be_present_and_click(button_edit_xpath) selenium_util.wait_to_be_visible("//label[contains(.,'Institution')]") if accept: # Choose role role = "Expert" selenium_util.wait_to_be_present_and_click( "//select[@id='role']/option[contains(.,'" + role + "')]") selenium_util.wait_to_be_clickable_and_click( "//button[@type='submit' and contains(.,'Accept')]") else: selenium_util.wait_to_be_clickable_and_click( "//button[@type='submit' and contains(.,'Deny creation')]") cancelButton = "//button[contains(.,'Cancel')]" if (selenium_util.check_exists_by_xpath(cancelButton)): selenium_util.wait_to_be_clickable_and_click(cancelButton)
def test_upload1(driver): driver.get("http://ui.yansl.com/#/upload") sleep(2) file = driver.find_element_by_xpath("//*[@id='form']/form/div[1]/div/input") file.clear() file.send_keys("D:\\softwareback\\eval.png") # 上传文件 sleep(5)
def test_hover(driver): time.sleep(2) zhi_nan = driver.find_element_by_xpath('(//span[text()="指南"])[last()]') action = ActionChains(driver) action.move_to_element(zhi_nan) action.perform() time.sleep(2)
def test_input(driver): # 定义一种方法 driver.get('http://ui.yansl.com/#/input') # 获取到此网址 sleep(2) # 停留2秒 input = driver.find_element_by_xpath("//input[@name='t1']") # 通过xpath定位元素:定位到输入框 input.clear() # 清空内容 input.send_keys("我是谁?我在哪?")# 输入框内输入 sleep(2)
def test_input(driver): driver.get('http://ui.yansl.com/#/dateTime') sleep(2) t1 = driver.find_element_by_xpath("//*[@id='form']/form/div[1]/div[1]/div/div/input") t1.clear() #清空 t1.send_keys("15:10:20") # 输入时间 sleep(5)
def test_slider(driver): driver.get("http://ui.yansl.com/#/slider") sleep(2) slider = driver.find_element_by_xpath("//*[@id='form']/form/div[5]/div/div/div/div[2]/div") sleep(2) actions = ActionChains(driver)# 类的实例化 #actions.move_to_element(slider).click_and_hold(slider).move_by_offset(0,-200).release(slider).perform(slider) actions.drag_and_drop_by_offset(slider,0,-200).perform() # 鼠标拖拽元素,横坐标,纵坐标 sleep(3)
def test_blog(self): driver = self.driver driver.maximize_window() driver.get("https://byzantium-travel.herokuapp.com/home/") time.sleep(1) elem = driver.find_element_by_xpath( "/html/body/nav/nav[2]/div/div/ul[1]/li[1]/a").click() driver.get("https://byzantium-travel.herokuapp.com/home/#Features") time.sleep(10)
def test_button(driver): driver.get("http://192.168.1.128:8082/xuepl/demo.html") sleep(2) button = driver.find_element_by_xpath("/html/body/table/tbody/tr[6]/td[2]/input") button.click() sleep(2) alert = driver.switch_to.alert # 弹框切换,切换到弹框里 alert.send_keys("nihao") # 输入nihao alert.accept() #点击确认 sleep(10)
def get_all_hikes(url): '''input: URL output: fully loaded webpage with all hike links for URL''' driver = webdriver.Chrome(chromedriver) driver.get(url) # Selects Decline in pop-up driver.find_element_by_xpath("//a[contains(text(),'Decline')]").click() count = 0 while count < 60: try: load_more_hikes = WebDriverWait(driver, 8).until(EC.visibility_of_element_located((By.XPATH,"//div[@id='load_more'] [@class='feed-item load-more trail-load'][//a]"))) # clicks to load more load_more_hikes.click() time.sleep(2) count += 1 except: break soup = BeautifulSoup(driver.page_source, 'lxml') return soup
def test_splider2(driver): time.sleep(1) splider = driver.find_element_by_xpath(''' //label[text()="竖向选择"]/../div//div[@class="el-tooltip el-slider__button"] ''') action = ActionChains(driver) action.drag_and_drop_by_offset(splider, 0, 90).perform() action.reset_actions() time.sleep(2) action.drag_and_drop_by_offset(splider, 0, -90).perform() action.reset_actions()
def test_text(driver): driver.get("http://ui.yansl.com/#/message") # 定位到“自动关闭提示”中的“消息”模块。多元素+s,elements buttons = driver.find_elements_by_xpath("//label[contains(text(),'自动关闭提示')]/..//span[text()='消息']") buttons[0].click() # 点击消息 # 定位到点击消息后显示的提示信息模块 message = driver.find_element_by_xpath("//div[@role='alert']/p") text = message.text # text+ 方法 获取展示文本 print(text) assert "这是一条消息" in text # 断言assert “内容”in next sleep(2)
def test_verify_registration(environment_setup): wait = WebDriverWait(driver, 100) # explicitly wait wait.until(ec.text_to_be_present_in_element(By.ID, 'sex', 'Male')) # explicitly wait obj = Select(driver.find_element_by_name("sex")) # maximizing window driver.maximize_window() # # enter text in input field driver.find_element_by_name("fld_username").send_keys("hello") time.sleep(20) # it waits for 2 sec after entering hello driver.find_element_by_name("fld_email").send_keys("*****@*****.**") driver.find_element_by_name("fld_password").send_keys("Abc123") driver.find_element_by_name("fld_cpassword").send_keys("Abc123") driver.find_element_by_name("fld_username").clear() driver.find_element_by_name("fld_username").send_keys("hello") driver.find_element_by_name("dob").send_keys("") # Working on Radio button driver.find_element_by_xpath("//input[@value='office']").click() # checkbox click driver.find_element_by_name("terms").click() # Seleck dropdoen obj = Select(driver.find_element_by_name("sex")) # select using visible text # obj.select_by_visible_text("Female") # select using index number obj.select_by_index(1) # select using value string # obj.select_by_value("2") # click button driver.find_element_by_xpath("//input[@type='submit']").click() # Link text driver.find_element_by_link_text("Read Detail").click() # Close Browser # driver.close() assert driver.current_url == "http:\\theTestingtutor.com"
def test_mouseOperations(): path = "C:\\selenium\\chromedriver.exe" driver = Chrome(executable_path=path) driver.get("http://www.theTestingWorld.com") act = ActionChains(driver) # act.click().perform() # left click where-ever # act.context_click().perform() # Right Click WhereEver # act.click(driver.find_element_by_xpath("\\a[text()='Home']")).perform() act.move_to_element(driver.find_element_by_xpath( "\\span[@contains(text(),'TUTORIAL')]")).perform()
def test_upload2(driver): driver.get("http://ui.yansl.com/#/upload") sleep(2) file = driver.find_element_by_xpath('//*[@id="form"]/form/div[2]/div/div/div[1]/button/span') file.click() sleep(3) autoit.win_wait("打开", 10) sleep(1) # autoit.control_send("打开", "Edit1", os.path.abspath(file_path)) autoit.control_set_text("打开", "Edit1", "D:\\softwareback\\eval.png") sleep(5) autoit.control_click("打开", "Button1") sleep(10)
def test_blog(self): driver = self.driver driver.maximize_window() driver.get("https://byzantium-travel.herokuapp.com/home/") time.sleep(1) #open Read More elem = driver.find_element_by_xpath("/html/body/div[1]/div/div/button").click() time.sleep(1) driver.get("https://byzantium-travel.herokuapp.com/home/#Features") time.sleep(2)
def buildrank(type): field = rankings[type]['field'] label = rankings[type]['label'] driver.find_element_by_xpath(f"//div[@class='nba-stat-table']//table//thead//tr//th[@data-field='{field}']").click() element = driver.find_element_by_xpath("//div[@class='nba-stat-table']//table") html_content = element.get_attribute('outerHTML') # Tratando o conteudo HTML soup = BeautifulSoup(html_content, 'html.parser') table = soup.find(name='table') # Estruturar conteudo em um DataFrame - Pandas df_full = pd.read_html(str(table))[0].head(10) df = df_full[['Unnamed: 0', 'PLAYER', 'TEAM', label]] df.columns = ['pos', 'Player', 'Team', 'Total'] # Transformando os dados em um Dicionãrio de dados Próprio return df.to_dict('records')