def relatorio(self): driver = self.driver driver.get('https://marketplace.integracommerce.com.br/v2/Order') time.sleep(4) usuario = driver.find_element_by_id("username") usuario.send_keys(self.user) first_option = WebDriverWait(driver, 10).until( EC.visibility_of_element_located(( By.XPATH, "/html/body/div[1]/div[2]/div/div/div[1]/div/form/div[1]/label" ))) first_option.click() password = driver.find_element_by_id("password") password.send_keys(self.passw) first_option = WebDriverWait(driver, 10).until( EC.visibility_of_element_located(( By.XPATH, "/html/body/div[1]/div[2]/div/div/div[1]/div/form/div[1]/label" ))) first_option.click() time.sleep(2) login = driver.find_element_by_xpath('//*[@id="kc-login"]').click() time.sleep(10) fecharmodal = WebDriverWait(driver, 15).until( EC.visibility_of_element_located( (By.CSS_SELECTOR, ".bootbox-close-button"))) fecharmodal.click() time.sleep(3) ordens = WebDriverWait(driver, 10).until( EC.visibility_of_element_located( (By.CSS_SELECTOR, "#intPedidosV2 > a:nth-child(1)"))) ordens.click() time.sleep(5) driver.switch_to.frame( driver.find_element_by_css_selector( '.container-fluid > iframe:nth-child(1)')) data = driver.find_element_by_xpath( "/html/body/div/div/div[2]/div/form/div[1]").click() time.sleep(5) data = driver.find_element_by_xpath( "/html/body/div/div/div[2]/div/form/div[2]/div/div/div[2]/div/div/div[1]/button[3]" ).click() data = driver.find_element_by_xpath( "/html/body/div/div/div[2]/div/form/div[2]/div/div/div[2]/div/div/div[1]/button[3]" ).click() data = driver.find_element_by_xpath( "/html/body/div/div/div[2]/div/form/div[2]/div/div/div[2]/div/div/div[2]/div/div/button[8]" ).click() data = driver.find_element_by_xpath( "/html/body/div/div/div[2]/div/form/div[2]/div/div/div[2]/div/div/div[2]/div/div/button[1]" ).click() data = driver.find_element_by_xpath( "/html/body/div/div/div[2]/div/form/div[2]/div/div/div[2]/div/div/div[2]/div/div/div/div[2]/button[1]" ).click() data = driver.find_element_by_xpath( "/html/body/div/div/div[2]/div/form/div[2]/div/div/div[2]/div/div/div[2]/div/div/div/div[2]/button[3]" ).click() data = driver.find_element_by_xpath( "/html/body/div/div/div[2]/div/form/button[2]").click()
def index_page(page): """ 抓取索引页 :param page: 页码 """ print('正在抓取第', page, '页') try: url = 'https://s.taobao.com/search?q=' + quote(KEYWORD) print(url) browser.get(url) if page > 1: input = wait.until( EC.presence_of_element_located( (By.CSS_SELECTOR, '#mainsrp-pager div.form > input'))) submit = wait.until( EC.element_to_be_clickable( (By.CSS_SELECTOR, '#mainsrp-pager div.form > span.btn.J_Submit'))) input.clear() input.send_keys(page) submit.click() wait.until( EC.text_to_be_present_in_element( (By.CSS_SELECTOR, '#mainsrp-pager li.item.active > span'), str(page))) wait.until( EC.presence_of_element_located( (By.CSS_SELECTOR, '.m-itemlist .items .item'))) get_products() except TimeoutError: index_page(page)
def __init__(self, driver): self.driver = driver self.about_me_header = WebDriverWait(self.driver.instance, 10).until( EC.visibility_of_element_located( (By.XPATH, "//h1[contains(text(), 'About Me')]"))) self.about_me_post = WebDriverWait(self.driver.instance, 10).until( EC.visibility_of_element_located((By.CLASS_NAME, "entry-content")))
def validate_social_media_links(self): twitter_button = WebDriverWait(self.driver.instance, 10).until( EC.visibility_of_element_located(( By.XPATH, "//span[contains(text(), 'Twitter')]"))) linkedin_button = WebDriverWait(self.driver.instance, 10).until( EC.visibility_of_element_located(( By.XPATH, "//span[contains(text(), 'LinkedIn')]"))) assert twitter_button.is_displayed() assert linkedin_button.is_displayed()
def log_in(self, ): try: # todo: dealing with popup windows # cookies popup print('\nClosing cookies window ...') WebDriverWait(self.driver, 10).until( EC.visibility_of_element_located( (By.XPATH, '//div/div/div[2]/button[1]'))).click() sleep(1) except Exception: self.error = True print('Unable to close popup window') else: try: with open(self.keys, 'r') as a: keys_dict = json.loads(a.read()) # Press button to access the login fields sleep(1) login_button = self.driver.find_element_by_xpath( '//section[2]/div[1]/div/section[2]/a[1]') login_button.click() sleep(1) # todo: switch window # https://sqa.stackexchange.com/questions/13792/how-to-proceed-after-clicking-a-link-to-new-page-in-selenium-in-python window_after = self.driver.window_handles[1] self.driver.switch_to.window(window_after) print('Logging in with username and password ...') WebDriverWait(self.driver, 10).until( EC.presence_of_element_located( (By.XPATH, '//*[@id="username"]'))) user_name_input = self.driver.find_element_by_xpath( '//*[@id="username"]') user_name_input.send_keys(keys_dict['username'][1]) sleep(1) WebDriverWait(self.driver, 10).until( EC.presence_of_element_located( (By.XPATH, '//*[@id="password"]'))) password_input = self.driver.find_element_by_xpath( '//*[@id="password"]') password_input.send_keys(keys_dict['password'][1]) sleep(1) a.close() # user_name_input.submit() self.driver.find_element_by_xpath( '//form/div[3]/button').submit() sleep(1) except Exception as e: print(e) self.error = True
def staff_directory(self): HR = WebDriverWait(self.driver, 40).until( EC.element_to_be_clickable( (By.LINK_TEXT, Dlocator.HR_sidemenu_linktext))) action = ActionChains(self.driver) action.move_to_element(HR).perform() self.driver.implicitly_wait(7) Staff_directory = WebDriverWait(self.driver, 60).until( EC.element_to_be_clickable( (By.LINK_TEXT, Dlocator.Staffdirectory_sidemenu_linktext))) Staff_directory.click()
def step_impl(context, Username, password): WebDriverWait(context.driver, 10).until(EC.element_to_be_clickable((By.ID, 'i0116'))) Log.Search_Username_Textbox(context).clear() Log.Search_Username_Textbox(context).send_keys(Username) Log.Search_Nextstep_Button(context).click() WebDriverWait(context.driver, 10).until(EC.element_to_be_clickable((By.ID, 'i0118'))) Log.Search_Password_Textbox(context).clear() Log.Search_Password_Textbox(context).send_keys(password) Log.Search_Signin_Button(context).click()
def find_name_send(self, selenium, name, number): try: WebDriverWait(selenium, 10, 0.5).until( EC.visibility_of_element_located((By.NAME, name))) selenium.find_element_by_name(name).send_keys(number) except: print("%s元素不可见" % name)
def find_id_send(self, selenium, id, number): try: WebDriverWait(self, selenium, 10, 0.5).until( EC.visibility_of_element_located((By.ID, id))) selenium.find_element_by_id(id).send_keys(number) except: print("%s元素不可见" % id)
def test_touzijjy(self): datas = ExcelReader (self.excel).data tzusers=ExcelReader(self.user).data tzbidds=ExcelReader(self.bidd).data for d in datas: with self.subTest (data=d): driver=webdriver.Chrome() driver.get('http://192.168.1.249:9901/hkjf/index.do?method=getIndexPage') WebDriverWait (driver, 10).until (EC.presence_of_element_located ((By.ID, 'login'))) for u in tzusers: with self.subTest(tzuser=u): driver.find_element_by_id ('login').send_keys (int(u['user'])) time.sleep(1) e1 = driver.find_element_by_xpath (".//*[@id='txt2']") action = ActionChains (driver) action.move_to_element (e1).click ().send_keys ("2971055a690ad019e9fc08a9971080ccfd6a8b588c69acc28383a12d9cfdcb135a60550a4df643b9967c5fab90ce4eb8e3970c2c093fefe299662ac44e868763d281e8708ab625528d55c6a777b2700bcb9daf7e7e0c6805ffd13760d4ac0120d6f43c2dc05fc38fcff485eedd8859d79200ddb7a9a606b8548fa1d8def1dacc").perform () driver.find_element_by_xpath(".//*[@id='logindiv']/div/div[2]").submit() for b in tzbidds: with self.subTest (tzbidd=b): # address=("http://192.168.1.249:9901/hkjf/investControllerFront.do?method=detail&code=ed791706-92f7-4435-b6f6-aa944ed87f4c") ad1 = "http://192.168.1.249:9901/hkjf/investControllerFront.do?method=detail&code=" ad2 = b['bidd_code'] add = ad1 + ad2 driver.get(add) time.sleep(1) # driver.find_element_by_id ("projectCode").send_keys (d['title']) driver.find_element_by_id("amount").send_keys(int(d['amount'])) driver.find_element_by_link_text("立即投资").click() time.sleep(2) #sreach_window=driver.current_window_handle #driver.find_element_by_xpath(".//*[@id='PDF9hFVQpC']/div[1]/div[3]/input[1]").click() driver.find_element_by_class_name("dialogBtn").click()
def test_bftz(): driver = webdriver.Chrome() driver.get( 'http://192.168.1.249:9901/hkjf/index.do?method=getIndexPage') WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.ID, 'login'))) driver.find_element_by_id('login').send_keys('13301307172') time.sleep(1) e1 = driver.find_element_by_xpath(".//*[@id='txt2']") action = ActionChains(driver) action.move_to_element(e1).click().send_keys( "2971055a690ad019e9fc08a9971080ccfd6a8b588c69acc28383a12d9cfdcb135a60550a4df643b9967c5fab90ce4eb8e3970c2c093fefe299662ac44e868763d281e8708ab625528d55c6a777b2700bcb9daf7e7e0c6805ffd13760d4ac0120d6f43c2dc05fc38fcff485eedd8859d79200ddb7a9a606b8548fa1d8def1dacc" ).perform() driver.find_element_by_xpath( ".//*[@id='logindiv']/div/div[2]").submit() time.sleep(1) driver.get( "http://192.168.1.249:9901/hkjf/investControllerFront.do?method=detail&code=477a34ea-b344-49fa-8dc2-b256b7502079" ) time.sleep(1) # driver.find_element_by_id("amount").send_keys("100") # driver.find_element_by_xpath(".//*[@id='clientInvestForm']/div[4]/a").click() driver.find_element_by_link_text("立即投资").click() time.sleep(3) #sreach_window=driver.current_window_handle #driver.find_element_by_xpath(".//*[@id='PDF9hFVQpC']/div[1]/div[3]/input[1]").click() driver.find_element_by_class_name("dialogBtn").click()
def find_css_send(self, selenium, css, number): try: WebDriverWait(selenium, 10, 0.5).until( EC.visibility_of_element_located((By.CSS_SELECTOR, css))) selenium.find_element_by_css_selector(css).send_keys(number) except: print("%s元素不可见" % css)
def checkZipcodeValidation(self, zipcode=None): element_present = EC.element_to_be_clickable((By.ID, "zip-code-popup-opener")) WebDriverWait(self.driver, 10).until(element_present) self.driver.find_element_by_id("zip-code-popup-opener").click() time.sleep(3) self.driver.find_element_by_id("zipcode").send_keys(zipcode) self.driver.find_element_by_id("zipcode").send_keys(Keys.ENTER)
def find_name_click(self, selenium, name): try: WebDriverWait(selenium, 10, 0.5).until( EC.visibility_of_element_located((By.NAME, name))) selenium.find_element_by_name(name).click() except: print("%s元素不可见" % name)
def find_xpath_send(self, selenium, xpath, number): try: WebDriverWait(selenium, 10, 0.5).until( EC.visibility_of_element_located((By.XPATH, xpath))) selenium.find_element_by_xpath(xpath).send_keys(number) except: print("%s元素不可见" % xpath)
def test_BiziPix_using(self): driver = self.driver driver.get(self.base_url) LoginPage.loginAction(self, 'SellerTestcjd', 'Ss123456') time.sleep(3) HelperTestBase.reliableClick(self, "[data-test-id='shoppingListProduct_0']") HelperTestBase.reliableClick(self, "[data-test-id='abstractListProduct_0']") HelperTestBase.reliableClick(self, "[data-test-id='BiziPix']") wait = WebDriverWait(self.driver, 100) cursor = wait.until( EC.visibility_of_element_located( (By.CSS_SELECTOR, "[data-test-id='cursor']"))) # img = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "[data-test-id='bizipixImage']"))) actions = ActionChains(driver) actions.move_to_element(cursor) actions.click_and_hold(cursor) actions.drag_and_drop_by_offset(cursor, 162, -75) time.sleep(2) actions.drag_and_drop_by_offset(cursor, 60, -80) time.sleep(2) actions.drag_and_drop_by_offset(cursor, 62, -75) actions.perform() time.sleep(3) HelperTestBase.reliableClick(self, "[data-test-id='bizipixCard']") time.sleep(6) self.assertIs( True, HelperTestBase.checkElementPresent( self, "[data-test-id='detailBuyNow']"))
def is_not_element_present(self, how, what, timeout=4): try: WebDriverWait(self.browser, timeout).until( EC.presence_of_element_located((how, what))) except TimeoutException: return True return False
def is_disappeared(self, how, what, timeout=4): try: WebDriverWait(self.browser, timeout, 1, TimeoutException). \ until_not(EC.presence_of_element_located((how, what))) except TimeoutException: return False return True
def find_id_click(self, selenium, id): try: WebDriverWait(selenium, 10, 0.5).until( EC.visibility_of_element_located((By.ID, id))) selenium.find_element_by_id(id).click() except: print("%s元素不可见" % id)
def find_elements(self, *loc): try: WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(loc)) return self.driver.find_elements(*loc) except NoSuchElementException: log.error("未找到元素") self.get_img()
def find_xpath_click(self, selenium, xpath): try: WebDriverWait(selenium, 10, 0.5).until( EC.visibility_of_element_located((By.XPATH, xpath))) selenium.find_element_by_xpath(xpath).click() except: print("%s元素不可见" % xpath)
def test_touziyyy(self): datas = ExcelReader(self.excel).data tzusers = ExcelReader(self.user).data tzbidds = ExcelReader(self.bidd).data for d in datas: with self.subTest(data=d): driver = webdriver.Firefox driver.get('http://192.168.1.249:8602/financial/index.html') WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.ID, 'loginTmp'))) for u in tzusers: with self.subTest(tzuser=u): # driver = webdriver.Chrome () # driver.get ('http://192.168.1.249:9901/hkjf/index.do?method=getIndexPage') # WebDriverWait (driver, 10).until (EC.presence_of_element_located ((By.ID, 'login'))) driver.find_element_by_id('loginTmp').send_keys( int(u['user'])) time.sleep(1) e1 = driver.find_element_by_id("passwdTmp").send_keys( "a12345") # action = ActionChains (driver) # action.move_to_element (e1).click ().send_keys ("2971055a690ad019e9fc08a9971080ccfd6a8b588c69acc28383a12d9cfdcb135a60550a4df643b9967c5fab90ce4eb8e3970c2c093fefe299662ac44e868763d281e8708ab625528d55c6a777b2700bcb9daf7e7e0c6805ffd13760d4ac0120d6f43c2dc05fc38fcff485eedd8859d79200ddb7a9a606b8548fa1d8def1dacc").perform () driver.find_element_by_id("loginBtn").submit() time.sleep(10)
def get_number_of_friends_from_user(self, user_id): """ Method finds the text on a profile page "Friends <num>" on side bar of 'Intro' (not always there), 'Photos' and 'Friends'. :param user_id: User profile page :return: The number of friends, or 'User Blocked' if we can't see """ self._facebook_login() self.driver.get('https://www.facebook.com/' + user_id) time.sleep(2) self.driver.execute_script( "window.scrollTo(0, document.body.scrollHeight);") try: self.driverWait.until( EC.presence_of_element_located((By.CLASS_NAME, '_39g5'))) except TimeoutException: print("Loading page took too long - timeout exception.") # Get list of spans ('Intro', 'Photos', 'Friends') friends_span = self.driver.find_elements_by_xpath( "//span[@class='_2iel _5kx5']") number_of_friends_span = self.driver.find_elements_by_xpath( "//a[@class='_39g5']") # Get number of friends. if len(friends_span) == 0: print('Page - No Friends') return 'Page - No Friends' if len(number_of_friends_span) == 0: print('User Blocked') return 'User Blocked' print(number_of_friends_span[0].text) return number_of_friends_span[0].text
def clicklogOutButton(self): wait = WebDriverWait(self.driver, 100) element = wait.until( EC.visibility_of_element_located( (By.CSS_SELECTOR, "[data-test-id='logout']"))) element.click() time.sleep(2)
def is_visible(self, locator, timeout=10): try: ui.WebDriverWait(self.driver, timeout).until( EC.visibility_of_element_located((By.XPATH, locator))) return True except TimeoutException: return False
def clickBackIcon(self): wait = WebDriverWait(self.driver, 100) element = wait.until( EC.visibility_of_element_located( (By.CSS_SELECTOR, "[data-test-id='backLink']"))) element.click() time.sleep(2)
def google_parse(self,index): driver = webdriver.Chrome() driver.get(self.google_url[index]) delay = 3 # seconds try: myElem = WebDriverWait(driver, delay).until(EC.presence_of_element_located( (By.CLASS_NAME, "section-layout.section-scrollbox.scrollable-y.scrollable-show"))) print("Page is ready!") except TimeoutException: print("Loading took too much time!") element = driver.find_element_by_class_name("section-layout.section-scrollbox.scrollable-y.scrollable-show") print(element) last_height =new_height=869 result="" while (True): new_height += 869 driver.execute_script("""arguments[0].scrollTo(arguments[2],arguments[1]);""", element, new_height, last_height) WebDriverWait(driver, 2) time.sleep(2) if new_height > driver.execute_script("return arguments[0].scrollHeight;", element): elements = driver.find_elements_by_class_name("section-review-text") break last_height = new_height for elem in elements: if elem.text != "": result+=elem.text + "***" check=result.split("***") #print(len(check)) return result
def test_optional_feedback(browser, link): browser.get(link) answer = send_answer() WebDriverWait(browser, 7).until( EC.visibility_of_element_located((By.CLASS_NAME, "textarea.string-quiz__textarea.ember-text-area.ember-view")) ) text_input = browser.find_element_by_class_name("textarea.string-quiz__textarea.ember-text-area.ember-view") text_input.send_keys(str(answer)) tap_button = browser.find_element_by_class_name("submit-submission") tap_button.click() hint = WebDriverWait(browser, 5).until( EC.visibility_of_element_located((By.CLASS_NAME, "smart-hints__hint")) ) alert = hint.text assert alert == "Correct!", f"Alert '''{alert}''' don't match with correct string"
def is_disappeared(self, how, what, timeout=4): # будет ждать до тех пор, пока элемент не исчезнет. try: WebDriverWait(self.browser, timeout, 1, TimeoutException). \ until_not(EC.presence_of_element_located((how, what))) except TimeoutException: return False return True
def test_create_prog(browser, test_singin): browser.get('https://programs.stage.incase.work/programs/new') time.sleep(2) WebDriverWait(browser, 10).until( EC.text_to_be_present_in_element((By.CLASS_NAME, "title"), "Program details")) browser.find_element_by_id('program-name').send_keys("erererrwr") browser.find_element_by_id('program-description').send_keys( 'Test1, erewrerTest1, erewrerTest1, erewrerTest1, ' 'erewrerTest1, erewrerTest1, erewrerTest1, ' 'erewrerTest1, erewrerTest1, erewrerTest1, ' 'erewrerTest1, erewrerTest1, erewrerTest1, ' 'erewrerTest1, erewrerTest1, erewrerTest1, ' 'erewrerTest1, erewrerTest1, erewrerTest1, ' 'erewrerTest1, erewrerTest1, erewrerTest1, ' 'erewrerTest1, erewrerTest1, erewrerTest1, ' 'erewrerTest1, erewrerTest1, erewrerTest1, ' 'erewrerTest1, erewrerTest1, erewrer') browser.find_element_by_css_selector( '#app > div > div > main > div.content > button').click() WebDriverWait(browser, 10).until( EC.text_to_be_present_in_element((By.CLASS_NAME, "heading-4"), "Choose Card Type")) browser.find_element_by_xpath( '//*[@id="app"]/div/div/main/div[2]/div/div[1]/div[1]').click() browser.find_element_by_css_selector( '#app > div > div > main > div.wrapper-steps > button.button.primary.large.continue' ).click() WebDriverWait(browser, 10).until( EC.text_to_be_present_in_element((By.CLASS_NAME, "heading-4"), "Card design")) file_imput = browser.find_element_by_xpath( '//*[@id="app"]/div/div/main/div[2]/div[1]/div[3]/input') file_imput.send_keys('/home/mark/Downloads/img') WebDriverWait(browser, 10).until( EC.invisibility_of_element( (By.CLASS_NAME, "button primary-outline large btn"))) browser.find_element_by_xpath( '//*[@id="app"]/div/div/main/div[2]/div[1]/button[1]').click() browser.find_element_by_id('card-title').send_keys("retretrete") browser.find_element_by_id('card-description').send_keys("ereretrt") browser.find_element_by_id('voucher-value').send_keys('5') element = browser.find_element_by_xpath( '//*[@id="app"]/div/div/main/div[2]/button[2]') browser.execute_script( "return arguments[0].scrollIntoView(0, document.documentElement.scrollHeight-10);", element) time.sleep(2) element.click()