def choose_page(self, key): '''分页操作''' pange_number = 15 # 每页展示的数据,默认是15条 driver = self.conf.driver driver.implicitly_wait(10) # 获取分页的数量 xpath = LC.COMMON[key] # 页面总数所在的xpath的路径值 totle = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, xpath))).text totles = int(totle) print('totles is :', totles) # 计算页数 pages = int(totles / pange_number) sum0 = driver.find_elements_by_xpath(xpath="//tr[@class='ng-scope']") sum = len(sum0) time.sleep(1) i = 0 for i in range(0, pages): # import pdb # pdb.set_trace() xpath1 = '//*[@id="main-container"]/div[1]/div[2]/div[2]/div/div/div[4]/ul[1]/li[8]/a' ele2 = WebDriverWait(driver, 10).until(lambda x: x.find_element_by_xpath(xpath=xpath1)) ele2.click() time.sleep(0.5) count0 = driver.find_elements_by_xpath(xpath="//tr[@class='ng-scope']") count = len(count0) sum = sum + count time.sleep(0.5) print('sum is :', sum) i = i + 1 time.sleep(0.5) assert sum == totles, "列表总数不正确,分页校验失败" print("总条数:%s,页数:%s" % (totles,pages)) print('列表总数正确,分页校验成功')
def wait(parent, timeout=TMO, base_id=None, retries=5): """ Wait for notification. Returns notification message. If `retries` <= 0 then we're checking for something we anticipate won't be there, so don't worry if it isn't. """ for retry in range(max(retries, 1)): time.sleep(0.5) # Pacing. base_id = base_id or 'notify' msg_id = base_id + '-msg' ok_id = base_id + '-ok' try: msg = WebDriverWait(parent.browser, timeout).until( lambda browser: browser.find_element(By.ID, msg_id)) ok = WebDriverWait(parent.browser, timeout).until( lambda browser: browser.find_element(By.ID, ok_id)) except WebDriverException as err: if retries > 0 or not isinstance(err, TimeoutException): logging.warning('NotifierPage.wait(%s): %r', base_id, err) else: # Sometimes the 'Ok' button is temporarily obscured. try: message = msg.text ok.click() return message except WebDriverException as err: logging.warning('NotifierPage.wait(%s): %r', base_id, err) if retries > 0 or not isinstance(err, TimeoutException): raise err
def title_contains(driver, title, timeout=3): try: w = WebDriverWait(driver, timeout) w.until(EC.title_contains(title)) return True except TimeoutException: return False
def is_found(driver, locator_type, locator, timeout=3): try: w = WebDriverWait(driver, timeout) w.until(lambda d: d.find_element(locator_type, locator)) return True except TimeoutException: return False
def getComments(self): """ Get the comments of the current picture. Returns: A list of comments of the picture, where a <comment> is a shortlist with the comment itself and the user of the comment. """ while True: try: more = WebDriverWait(self.driver, 1).until(EC.presence_of_element_located((By.ID, INFOS['viewMore']))) more.click() except: break soup = BeautifulSoup(self.driver.page_source, 'html.parser') myComments = [] allComments = soup.find('ol', {'id': INFOS['picComments']}).find_all('li') for comment in allComments: try: start = comment.find('div', {'class': 'h-mavatar'}) user = start.find('div').find('h3').find('a').getText() comm = start.find('p', {'class': INFOS['comment']}).getText() myComments.append([comm, user]) except: pass return myComments
def is_visible(driver, locator_type, locator, timeout=3): try: w = WebDriverWait(driver, timeout) w.until(EC.visibility_of_element_located((locator_type, locator))) return True except TimeoutException: return False
def hide_and_get(self): # hide current menu and get it again menu = WebDriverWait(self.driver, 30, 0.1).until( lambda d: d.find_element_by_css_selector(self.DROP_MENU_SELECT) ) menu.click() menu.click()
def testDragAndDrop(self): """Copied from org.openqa.selenium.interactions.TestBasicMouseInterface.""" element_available_timeout = 15 wait = WebDriverWait(self, element_available_timeout) self._loadPage("droppableItems") wait.until(lambda dr: dr._isElementAvailable("draggable")) if not self._isElementAvailable("draggable"): raise "Could not find draggable element after 15 seconds." toDrag = self.driver.find_element_by_id("draggable") dropInto = self.driver.find_element_by_id("droppable") holdDrag = ActionChains(self.driver) \ .click_and_hold(toDrag) move = ActionChains(self.driver) \ .move_to_element(dropInto) drop = ActionChains(self.driver).release(dropInto) holdDrag.perform() move.perform() drop.perform() dropInto = self.driver.find_element_by_id("droppable") text = dropInto.find_element_by_tag_name("p").text self.assertEqual("Dropped!", text)
def is_clickable(driver, full_xpath, xpath, timeout = 1): try: w = WebDriverWait(driver, timeout) w.until(EC.element_to_be_clickable(('xpath',xpath))) return XPathUtil.is_clickable(full_xpath) except TimeoutException, ElementNotVisibleException: return False
def testDragAndDrop(driver, pages): """Copied from org.openqa.selenium.interactions.TestBasicMouseInterface.""" element_available_timeout = 15 wait = WebDriverWait(driver, element_available_timeout) pages.load("droppableItems.html") wait.until(lambda dr: _isElementAvailable(driver, "draggable")) if not _isElementAvailable(driver, "draggable"): raise AssertionError("Could not find draggable element after 15 seconds.") toDrag = driver.find_element_by_id("draggable") dropInto = driver.find_element_by_id("droppable") holdDrag = ActionChains(driver) \ .click_and_hold(toDrag) move = ActionChains(driver) \ .move_to_element(dropInto) drop = ActionChains(driver).release(dropInto) holdDrag.perform() move.perform() drop.perform() dropInto = driver.find_element_by_id("droppable") text = dropInto.find_element_by_tag_name("p").text assert "Dropped!" == text
def isPresent(self): time.sleep(60) elem_projects_1 = self.driver.find_element_by_link_text("Projects") elem_projects_1 = WebDriverWait(self.driver, 20).until(lambda driver : driver.find_element_by_link_text("Projects")) elem_projects_1.click() time.sleep(10) self.assertIn(u"Projects", self.driver.title) #print self.driver.find_element_by_xpath('//*[@id="snap"]/div/div[5]') try: snap_finish = WebDriverWait(self.driver, 20).until(lambda d : d.find_elements_by_class_name('processingstep')) snap_png=snap_finish[3].get_attribute("style") except: print '1.catch the snap_finish error, and try again' try: snap_finish = WebDriverWait(self.driver, 20).until(lambda d : d.find_elements_by_class_name('processingstep')) snap_png=snap_finish[3].get_attribute("style") pass except: print '2. catch the snap_finish error, and try again' snap_finish = WebDriverWait(self.driver, 20).until(lambda d : d.find_elements_by_class_name('processingstep')) snap_png=snap_finish[3].get_attribute("style") pass print "snap_png is %s " % snap_png finish_flag = re.search("step_ok_active", snap_png, re.IGNORECASE) print "finish_flag is %s " % snap_finish error_flag = re.search("step_error_active", snap_png, re.IGNORECASE) #print "erro_flag is "+ not bool(error_flag) if bool(error_flag): return False else: if bool(finish_flag): return True
def test3(self): driver = self.driver driver.get("http://www.google.com") # find the element that's name attribute is q (the google search box) inputElement = driver.find_element_by_name("q") # type in the search inputElement.send_keys("Cheese!") # submit the form (although google automatically searches now without submitting) inputElement.submit() # the page is ajaxy so the title is originally this: print driver.title # we have to wait for the page to refresh, the last thing that seems to be updated is the title try: # element = WebDriverWait(driver, 10).until(EC.title_contains("cheese!")) #element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "gs_htif0"))) #print element.text # or wait = WebDriverWait(driver, 10) #element = wait.until(EC.presence_of_element_located((By.ID, "gs_htif0"))) element = wait.until(EC.element_to_be_clickable((By.ID,'gb_8'))) r = element.text print "r", r element1 = driver.find_element_by_xpath("//*[@id='gb_8']/span[2]") element1.click() title = driver.title print title except: pass
def __get__(self, obj, type): wdw = WebDriverWait(obj._driver, TIMEOUT) wdw.until( lambda driver: driver.find_elements(*self._locator), 'Element {} not found'.format(self._locator)) self._elements = obj._driver.find_elements(*self._locator) return self._elements
def _parse_statement(self): wait = WebDriverWait(self.driver, 5) try: wait.until(lambda driver: driver.find_element_by_name('SetofPages')) return self._parse_multipage() except: return self._parse_single_page()
def handle_popup(self,cancel): wait = WebDriverWait(self.selenium, self.timeout) wait.until(EC.alert_is_present()) # throws timeout exception if not found popup = self.selenium.switch_to_alert() answer = 'cancel' if cancel else 'ok' print popup.text + " ...clicking " + answer popup.dismiss() if cancel else popup.accept()
def getpage(pageurl, load_marker): """ Load pageurl using selenium webdriver, read and return the pagesource :param pageurl: :return: """ # Start the webdriver and load page wd = webdriver.Firefox() #wd = webdriver.Chrome('/usr/local/Cellar/chromedriver/2.20/bin/chromedriver') wd.get(pageurl) # Wait for the dynamically loaded elements (load_marker) to show up try: dt = 3 # seconds wait = WebDriverWait(wd, dt) wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, load_marker))) except TimeoutException: wd.quit() phtml = [] return phtml except UnexpectedAlertPresentException: wd.quit() phtml = [] return phtml # Read page and quit browser phtml = wd.page_source wd.quit() return phtml
def getupc(data, sleeptime): display = Display(visible=0, size=(800, 600)) display.start() a = webdriver.Firefox() a.get('https://www.google.com/ncr') time.sleep(sleeptime) search = WebDriverWait(a, 5).until(EC.element_to_be_clickable((By.XPATH, "//input[@type='text']"))) for i in data: ActionChains(a).move_to_element(search).click(search).send_keys(i['name'] + ' upc', Keys.ENTER).perform() time.sleep(sleeptime) contents = WebDriverWait(a, 5).until(EC.presence_of_all_elements_located((By.XPATH, "//div[@class='g']"))) try: upc = next( (re.split(r'/', href.find_element_by_tag_name('a').get_attribute('href'))[-1] for href in contents if href.find_element_by_tag_name('a').get_attribute('href').startswith( 'http://www.upcitemdb.com/upc'))) i['upc'] = upc except StopIteration: pass search = WebDriverWait(a, 5).until(EC.element_to_be_clickable((By.XPATH, "//input[@type='text']"))) search.clear() a.close() display.stop() return data
class MyPcSwitchViewsTest(unittest.TestCase): def setUp(self): """Open MyPC Login page and login as user "admin" without password""" self.driver = webdriver.Firefox() self.wait = WebDriverWait(self.driver, 10) self.driver.get("http://10.10.32.66/loadtest") assert "Performance Center" in self.driver.title login_name_field = self.wait.until(EC.element_to_be_clickable((By.ID, 'ctl00_PageContent_txtUserName'))) login_name_field.send_keys("admin") login_name_field.send_keys(Keys.RETURN) login_button = self.wait.until(EC.element_to_be_clickable((By.ID, 'ctl00_PageContent_btnLogin'))) login_button.click() time.sleep(5) self.driver.switch_to.window(self.driver.window_handles[-1]) def tearDown(self): self.driver.quit() def test_switch_to_test_plan_perspective(self): """Switch to test plan perspective after login""" driver = self.driver wait = WebDriverWait(driver, 10) test_perspective_button = wait.until(EC.element_to_be_clickable((By.XPATH, "/html/body/masthead/div/div[3]/div/div[2]/span[1]"))) actions = ActionChains(driver) actions.move_to_element(test_perspective_button) actions.perform() time.sleep(2) actions2 = ActionChains(driver) test_plan = wait.until(EC.presence_of_element_located((By.XPATH, "/html/body/masthead/div/div[3]/div/div[2]/ul/li[1]"))) actions2.move_to_element(test_plan) actions2.click(test_plan) actions2.perform() time.sleep(5)
def test_answer(self): for x in range(0,10): #self.browser.find_element_by_xpath("(//input[@name='ans'])[2]").click() toclick = WebDriverWait(self, 10).until(lambda self : self.browser.find_element_by_xpath("(//input[@name='ans'])[2]")) toclick.click() urgency = self.browser.find_element_by_id("userquestion_urgency") urgency.send_keys("9") importance = self.browser.find_element_by_id("userquestion_importance") importance.send_keys("10") self.browser.find_element_by_id("userquestion_changecat").click() category = self.browser.find_element_by_id("userquestion_category") category.send_keys("Strategy") self.browser.find_element_by_id("userquestion_changescope").click() #activescope = self.browser.find_element_by_id("userquestion_activescope") #activescope.select_by_visible_text("2 Continental") #continent = self.browser.find_element_by_id("userquestion_continent") #continent.select_by_visible_text("Africa (AF)") #self.browser.find_element_by_id("userquestion_answerreason").clear() self.browser.find_element_by_id("userquestion_answerreason").send_keys("test phase 4 user2") #driver.find_element_by_css_selector("input.btn").click() #answer.send_keys("1") submit_button = self.browser.find_element_by_css_selector("#submit_record__row input") submit_button.click() body = WebDriverWait(self, 10).until(lambda self : self.browser.find_element_by_tag_name('body')) self.assertIn('This question is in progress', body.text) self.browser.find_element_by_xpath("//input[@value='Next Question']").click()
class Task(object): def __init__(self, options): self.driver = webdriver.Chrome(driver_path) self.domain = options['domain'] self.wait = WebDriverWait(self.driver, 10) @staticmethod def dom_ready(driver): state = driver.execute_script('return document.readyState') return state == 'complete' def event_signup(self, event_id): driver = self.driver driver.get('%s/event?id=%s' % (self.domain, event_id)) self.wait.until(self.dom_ready) apply_elem = driver.find_element_by_class_name('join') apply_elem.click() # TODO 填值 报名 def login(self, username, password): driver = self.driver login_url = self.domain + '/login' driver.get(login_url) form = driver.find_element_by_class_name('loginform') e_username = form.find_element_by_name('username') e_password = form.find_element_by_name('password') e_username.send_keys(username) e_password.send_keys(password) form.submit() time.sleep(2)
def setup_prevalence_chart(self, disease_progression_name, points, timeout=None): """Sets up the prevalence chart for the given Disease Progression. Returns the name of the relational function that contains the prevalence chart.""" if timeout is None: timeout = self.default_timeout self.click_navbar_element('Disease Progression') # Once the Disease Progression choices come up, click the desired one WebDriverWait(self.selenium, timeout=timeout).until( EC.visibility_of_element_located((By.LINK_TEXT, disease_progression_name)) ).click() # Once the latent period, etc. options come up, click to bring up the prevalence chart prevalence_options = WebDriverWait(self.selenium, timeout=timeout).until( EC.visibility_of_element_located((By.ID, 'id_disease_prevalence')) ) prevalence_name = Select(prevalence_options).first_selected_option.text # remember its name prevalence_options.click() # Once the prevalence chart comes up, overwrite the old points with the new ones. FunctionsPanel( WebDriverWait(self.selenium, timeout=timeout).until( EC.visibility_of_element_located((By.ID, 'functions_panel')) ) ).set_points(points) return prevalence_name
def loginForPhone(self, phoneName, passWord): br = self.br name = self.phoneName password = self.passWord message1 = 'python返回一个值不能全部读取' element = loginElement.elementInit() we = findElementAPI # 判断页面元素是否加载完 wait = WebDriverWait(br, 10) wait.until(EC.presence_of_all_elements_located, "页面元素未加载完全") try: we.switchFrame(br,element[1]) we.findXpath(br,element[2]).send_keys(name) we.findXpath(br,element[4]).send_keys(password) we.findXpath(br,element[5]).click() except: srcName = ('登录操作失败,请检查环境是否正常') return srcName # 判断登录是否弹出框 br.implicitly_wait(3) try: srcName = we.findXpath(br,element[7]).text lenElement = 1 except: lenElement = 0 if lenElement == 0: try: srcName = we.findXpath(br,element[8]).text except: message1 = '未找到元素' return message1 return srcName, message1
def click_by_order_id(self,key,id): driver = self.conf.wxdriver #import pdb;pdb.set_trace() xpath = LC.ORDER[key] % str(id) el = WebDriverWait(driver, WAIT_TIME).until(EC.element_to_be_clickable((By.XPATH, xpath))) time.sleep(1) el.click()
class Action(): def __init__(self): self.desired_caps = { "platformName": "Android", "deviceName": "SM_G9500", "appPackage": "com.tencent.mm", "appActivity": ".ui.LauncherUI", "noReset": True } self.driver = webdriver.Remote(DRIVER_SERVER, self.desired_caps) self.wait = WebDriverWait(self.driver, TIMEOUT) def entry(self): # 点击进入搜索 search = self.wait.until(EC.presence_of_element_located((By.XPATH, '//android.widget.TextView[@content-desc="Search"]'))) search.click() # 点击输入搜索内容 keyword = self.wait.until(EC.presence_of_element_located((By.ID, 'com.tencent.mm:id/hx'))) keyword.set_text(KEYWORD) sleep(2) # 点击搜索 TouchAction(self.driver).tap(x=1299, y=2605).perform() sleep(2) # 点击公众号 TouchAction(self.driver).tap(x=672, y=634).perform() # 点击右上角人像 view_profile = self.wait.until(EC.presence_of_element_located((By.XPATH, '//android.widget.ImageButton[@content-desc="Chat Info"]'))) view_profile.click() # 点击查看历史 view_history = self.wait.until(EC.presence_of_element_located((By.XPATH, '//android.widget.LinearLayout[8]//*[@resource-id="android:id/title"]'))) view_history.click() sleep(3) # TouchAction(self.driver).press(x=806, y=2500).move_to(x=806, y=2400).release().perform() self.driver.swipe(FLICK_START_X, FLICK_START_Y + 960, FLICK_START_X, FLICK_START_Y) sleep(1) while True: t = -450 for i in range(6): try: t += 440 sleep(1) # 循环点击每篇文章图片 图片高度500px # x, y根据自己手机屏幕来调整 TouchAction(self.driver).tap(x=1019, y=440+t).perform() # 尝试再点击一次, 如果第一次点击到两个图片的中间, 并不会进入文章 # 图片与图片间隔180px try: TouchAction(self.driver).tap(x=1150, y=440+t+182).perform() except: pass # 点击退出文章 sleep(2) back = self.wait.until(EC.presence_of_element_located((By.ID, 'com.tencent.mm:id/i2'))) back.click() except: pass sleep(1) # 模拟拖动 self.driver.swipe(FLICK_START_X, FLICK_START_Y + 1500, FLICK_START_X, FLICK_START_Y)
def check_scores(self, user, passwd, score, rating, questions): mailstring = user + '@user.com' email = WebDriverWait(self, 10).until(lambda self: self.browser.find_element_by_name("email")) email.send_keys(mailstring) password = self.browser.find_element_by_name("password") #password.send_keys(USERS['PASSWORD2']) password.send_keys(passwd) submit_button = self.browser.find_element_by_css_selector("#submit_record__row input") submit_button.click() time.sleep(1) scorestring = 'Score: ' + str(score) ratingstring = 'Rating: ' + str(rating) questionstring = 'Questions: ' + str(questions) time.sleep(1) body = WebDriverWait(self, 10).until(lambda self: self.browser.find_element_by_tag_name('body')) self.assertIn(scorestring, body.text) self.assertIn(ratingstring, body.text) self.assertIn(questionstring, body.text) self.url = ROOT + '/default/user/logout' get_browser = self.browser.get(self.url)
def test_statsAnimation(self): driver = self.driver # Load a test image with animation Util.load_image( self, driver, "Default") # Move to the center of the image window so data appears in the Stats Window imageWindow = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//div[@qxclass='skel.widgets.Window.DisplayWindowImage']"))) ActionChains(driver).move_to_element( imageWindow ).perform() # Get the Statistics of the loaded image statsText = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//div[@qxclass='skel.boundWidgets.Label']"))) statsText = statsText.get_attribute('textContent') # Click the forward animation button forwardAnimateButton = driver.find_element_by_xpath("//div[@class='qx-toolbar']/div[@class='qx-button'][2]") self.assertIsNotNone( forwardAnimateButton, "Could not find forward animation button") driver.execute_script( "arguments[0].scrollIntoView(true);", forwardAnimateButton) ActionChains(driver).click( forwardAnimateButton ).perform() time.sleep(3) # Move to the center of the image window so data appears in the Stats Window imageWindow = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//div[@qxclass='skel.widgets.Window.DisplayWindowImage']"))) ActionChains(driver).move_to_element( imageWindow ).perform() # Sometimes text does not appear, therefore move mouse cursor ActionChains(driver).move_by_offset( 100, 100 ).perform() # Get the Statistics of the loaded image newStatsText = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//div[@qxclass='skel.boundWidgets.Label']"))) newStatsText = newStatsText.get_attribute('textContent') # Check that the Statistics text changed when image in the image window was changed self.assertNotEqual( newStatsText, statsText, "Stats text did not update during animation of the image")
def test_statsAddLink(self): driver = self.driver time.sleep(5) # Load a different image in a different window imageWindow2 = Util.load_image_different_window( self, driver, "aK.fits") # Find and click on the Statistics window statsWindow = driver.find_element_by_xpath("//div[@id='Statistics']") self.assertIsNotNone( statsWindow, "Could not find statistics window") ActionChains(driver).click( statsWindow ).perform() # In Stastics context menu, open Link Settings ActionChains(driver).context_click( statsWindow ).send_keys(Keys.ARROW_DOWN).send_keys(Keys.ARROW_DOWN).send_keys(Keys.ENTER).perform() # Add a link to the new image window imageWindow2 = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//div[@id='CasaImageLoader2']"))) ActionChains(driver).move_to_element( statsWindow ).click( statsWindow ).drag_and_drop( statsWindow, imageWindow2).perform() # Exit links ActionChains(driver).move_to_element( statsWindow ).context_click( statsWindow ).send_keys(Keys.ARROW_DOWN).send_keys(Keys.ENTER).perform() # Move to the center of the image window so data appears in the Stats Window ActionChains(driver).move_to_element( imageWindow2 ).perform() # Sometimes text does not appear, therefore move mouse cursor ActionChains(driver).move_by_offset( 100, 100 ).perform() # Check that the Stastics window is not linked to the main image window statsText = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//div[@qxclass='skel.boundWidgets.Label']"))) statsText = statsText.get_attribute('textContent') # Check that the Default sky text appears in the Stats Window statsText = statsText.startswith("Default sky") self.assertEqual( statsText, 0, "Statistics window should not be linked to multiple windows")
def wait_for_ajax_loading(browser, class_name): """ Waits until the ajax loading indicator disappears. """ wait = WebDriverWait(browser, 30) wait.until(lambda browser: len(browser.find_elements_by_class_name( class_name)) == 0)
def test_statsAddImage(self): driver = self.driver # Load a large test image. Util.load_image( self, driver, "aH.fits") # Move to the center of the image window so data appears in the Stats Window imageWindow = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//div[@qxclass='skel.widgets.Window.DisplayWindowImage']"))) ActionChains(driver).move_to_element( imageWindow ).perform() # Get the Statistics of the loaded image statsText = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//div[@qxclass='skel.boundWidgets.Label']"))) statsText = statsText.get_attribute('textContent') # Load a different image in the same window Util.load_image( self, driver, "aK.fits") # Move to the center of the image window so data appears in the Stats Window ActionChains(driver).move_to_element( imageWindow ).perform() # Sometimes text does not appear, therefore move mouse cursor ActionChains(driver).move_by_offset( 100, 100 ).perform() # Get the Statistics of the loaded image newStatsText = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//div[@qxclass='skel.boundWidgets.Label']"))) newStatsText = newStatsText.get_attribute('textContent') # Check that the Statistics text changed when new image was loaded self.assertNotEqual( newStatsText, statsText, "Stats text did not update when new image was loaded in main image window")
def testExpectedConditionVisibilityOfElementLocated(self, driver, pages): pages.load("javascriptPage.html") with pytest.raises(TimeoutException): WebDriverWait(driver, 0.7).until(EC.visibility_of_element_located((By.ID, 'clickToHide'))) driver.find_element_by_id('clickToShow').click() element = WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.ID, 'clickToHide'))) assert element.is_displayed() is True
data = pd.read_csv("./vaccination_data.csv") data.date = pd.to_datetime(data.date) last_date = data.iat[len(data) - 1, 0] # use a headless browser (saves time) chrome_options = Options() chrome_options.add_argument("--headless") chrome_options.add_argument("--start-maximized") # set up the browser driver = webdriver.Chrome(options=chrome_options) driver.get("https://www.covid19.act.gov.au/") # scrape date date_field = WebDriverWait(driver, 10).until( EC.visibility_of_element_located((By.CLASS_NAME, "spf-article-card--tabular-subtitle")) ) date_text = date_field.find_element_by_tag_name("p").get_attribute("innerText") date = date_text[date_text.index(",") + 2:] date = pd.to_datetime(date, format="%d/%m/%Y") # transform to date object # scrape vaccinations vaccination_field = WebDriverWait(driver, 10).until( EC.visibility_of_element_located((By.CSS_SELECTOR, ".col-lg-6:last-child td:last-child")) ) vaccinations = int(vaccination_field.get_attribute("innerText").replace(",", "")) # check if data needs update if date > last_date: print("This is new data — saving to file.") print(f"{vaccinations:,.0f} vaccinations as of {date:%B %d, %Y}.")
def find_position(keyword, siteHref): site = get_domain(siteHref).netloc if site == '': site = siteHref if site == '': return False search_box = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.NAME, "q"))) # Sometimes this search filed changes. # (may be google change it for security reasons) try: search_box.clear() time.sleep(1) search_box.send_keys(keyword) time.sleep(1.5) search_box.submit() except Exception as ee: print(str(ee)) time.sleep(10) driver.get('http://www.google.com/') time.sleep(20) print( 'entering recursion - search field raise an exception: Sleep 10 - enter google.com - sleep 20 sec and search again' ) return find_position(keyword, siteHref) link_results = [] # not used for now x = range(page_turns) current_site_index = False for n in x: time.sleep(2) print('searching page', n + 1) link_list = get_results() # print('list: ', link_list) link_results += link_list ad_found_at = None organic_found_at = None for index, result in enumerate(link_results): if result['href'] == None: continue linkLower = result['href'].lower() siteLower = site.lower() if linkLower.count(siteLower) > 0: if (result['ad']): ad_found_at = ad_found_at if ad_found_at else index print('[AD] found at position: ', ad_found_at) else: organic_found_at = organic_found_at if organic_found_at else index print('[ORGANIC] found at position: ', organic_found_at) # Both are found. So we break if organic_found_at: break else: # Clicking next_page() - if next page return false - no next page button exists (no more pages available) if n < page_turns: if next_page() == False: return False if ad_found_at or organic_found_at: # Adding results ot dict to process either later found_results = {'ad': None, 'organic': None} if ad_found_at: found_results['ad'] = link_results[ad_found_at] found_results['ad']['position'] = ad_found_at if organic_found_at: found_results['organic'] = link_results[organic_found_at] found_results['organic']['position'] = organic_found_at return {'results': found_results} else: return False
def test_TC003_upcoming_events_check(self): self.driver.get("https://www.python.org/about") css_selector = '#content > div > section > div.list-widgets.row > div.medium-widget.event-widget.last > div > h2' element = WebDriverWait(self.driver,10).until(EC.visibility_of_element_located((By.CSS_SELECTOR,css_selector))) assert element.text == 'Upcoming Events'
def do_fill(self, name, value): element = self.find_element(name) element.send_keys(value) WebDriverWait(self.driver, self.DELAY_AFTER_FILL)
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.ui import WebDriverWait b = webdriver.Firefox() url = "http://www.kobis.or.kr/kobis/business/stat/boxs/findFormerBoxOfficeList.do?loadEnd=0&searchType=search&sMultiMovieYn=&sRepNationCd=K&sWideAreaCd=" b.get(url) movies = b.find_elements_by_css_selector(".boxMNm") for movie in movies: movie_code = movie.get_attribute("onclick").split("'")[3] movie.click() WebDriverWait(b, 10).until( EC.presence_of_element_located((By.ID, movie_code + "_staff"))) movie_staffs = b.find_elements_by_css_selector(".peopContent2") movie_actors = movie_staffs[0].find_elements_by_css_selector(".peopContNm") for actor in movie_actors: print(actor) print(movie.text, movie_code) b.find_element_by_class_name("layer_close").click() b.quit()
otps = Options() otps.add_argument( "user-agent=Mozilla/5.0 (X11; linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko)" ) chrome_options = webdriver.ChromeOptions() experimentalFlags = ['calculate-native-win-occlusion@2'] chromeLocalStatePrefs = {'browser.enabled_labs_experiments': experimentalFlags} chrome_options.add_experimental_option('localState', chromeLocalStatePrefs) driver_path = 'C:\\Users\\frigh\\OneDrive\\Escritorio\\python\\chromedriver.exe' otps.add_argument('--no-sandbox') driver = webdriver.Chrome(driver_path) driver.get('https://twitter.com/home') # Login input_user = WebDriverWait(driver, 15).until((EC.presence_of_element_located( (By.XPATH, "//input[@name='session[username_or_email]' and @dir='auto']")))) input_pass = driver.find_element(By.XPATH, '//input[@name="session[password]"]') nombre = "" input_user.send_keys(nombre) password = "" input_pass.send_keys(password) boton = driver.find_element( By.XPATH, '//main//div[@data-testid="LoginForm_Login_Button"]//div[@dir="auto"]') boton.click() #URL followers driver.get('https://twitter.com/@example1/followers')
def cls_config(self): wait = WebDriverWait(self.driver, 10) return wait
from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By chrome_options = Options() chrome_options.add_argument('start-maximized') driver = webdriver.Chrome(options=chrome_options) driver.get('https://www.mvideo.ru/') items = [] for i in range(5): button = WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, "//h2[contains(text(), 'Новинки')]/ancestor::div[@class='section']//a[contains(@class, 'i-icon-fl-arrow-right')]"))) button.click() goods = driver.find_elements_by_xpath("//h2[contains(text(), 'Новинки')]/ancestor::div[@class='section']//a[@class = 'fl-product-tile-title__link sel-product-tile-title']") for g in goods: if g not in items: items.append(g) for x in items: print(x.get_attribute('data-product-info')) driver.close()
from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium import webdriver browser = webdriver.Chrome() browser.get("http://suninjuly.github.io/explicit_wait2.html") price = WebDriverWait(browser, 12).until(EC.text_to_be_present_in_element((By.ID, "price"), '$100')) book = browser.find_element_by_id("book") book.click() import math def calc(x): return str(math.log(abs(12*math.sin(int(x))))) x_element = browser.find_element_by_id('input_value') x = x_element.text y = calc(x) answer = browser.find_element_by_id('answer') answer.send_keys(y) submit = browser.find_element_by_css_selector('button[type="submit"]') submit.click()
options = webdriver.ChromeOptions() options.add_argument('--start-maximized') options.add_argument('--disable-extensions') ruta = os.getcwd() driver_path = "{}/chromedriver.exe".format(ruta) driver = webdriver.Chrome(driver_path,chrome_options=options) #Inicializamos el navegador driver.get("https://www.wplay.co/") #time.sleep(10) #Usamos ruta absoluta WebDriverWait(driver, 5)\ .until(EC.element_to_be_clickable((By.XPATH, '/html/body/div[1]/div/div/div[2]/div/div[1]/a')))\ .click() #$x("//div[@class='fragment expander coupon-for-type']//h4[contains(text(),'Ligue 1')]") #$x("//li[@class='expander expander-collapsed sport-FOOT']//span[contains(text(),'Futbol')]") WebDriverWait(driver, 5)\ .until(EC.element_to_be_clickable((By.XPATH, "//li[@class='expander expander-collapsed sport-FOOT']//span[contains(text(),'Futbol')]")))\ .click() #/html/body/div[1]/div/div[3]/div/div/div[1]/div[4]/ul/li[1]/ul/li[14]/div/span WebDriverWait(driver, 5)\ .until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[1]/div/div[3]/div/div/div[1]/div[4]/ul/li[1]/ul/li[14]/div/span")))\ .click()
def verifyLogo(): # wait until logo on the page is visible and verify its text logo = WebDriverWait(driver, 30).until( EC.visibility_of_element_located((By.CLASS_NAME, "tn-atom"))) assert logo.text == "DATAFOLKS"
#---------------->SETUP<------------------# mobile_emulation = {"deviceName": "iPhone 8"} chrome_options = webdriver.ChromeOptions() chrome_options.add_experimental_option("mobileEmulation", mobile_emulation) prefs = {"profile.default_content_setting_values.notifications": 2} chrome_options.add_experimental_option("prefs", prefs) driver = webdriver.Chrome( executable_path="C:\Program Files (x86)\chromedriver.exe", options=chrome_options) url = "https://www.hurriyetemlak.com" driver.get(url) driver.maximize_window() #tam ekran olsun try: kapat_kampanya = WebDriverWait(driver, 20).until( EC.presence_of_element_located((By.CLASS_NAME, "modal__close"))) finally: kapat_kampanya.click() #---------------->TEST<------------------# driver.get(driver.current_url + "kiralik") #filtrele Dropdown try: dropdown = WebDriverWait(driver, 20).until( EC.presence_of_element_located(( By.XPATH, "/html/body/div[1]/div/div/div[2]/div[1]/div[1]/div[3]/div/button[2]" ))) finally:
def run(self): self.driver.get('http://www.aliexpress.com/') try: WebDriverWait(self.driver, 40).until( EC.element_to_be_clickable((By.CLASS_NAME, 'close-layer')) ) self.driver.find_element_by_class_name('close-layer').click() except Exception: pass # for cookie in json.loads(item[3]): # self.driver.add_cookie(cookie) #页面刷新 # self.driver.refresh() # try: # WebDriverWait(self.driver, 40).until( # EC.element_to_be_clickable((By.CLASS_NAME, 'close-layer')) # ) # self.driver.find_element_by_class_name('close-layer').click() # except Exception: # pass # self.driver.execute_script('window.scrollTo(document.body.scrollWidth,0 );') # WebDriverWait(self.driver, 40).until( # EC.element_to_be_clickable((By.XPATH, '//div[@id="nav-user-account"]/div[1]/div[1]/span[1]/a[1]')) # ) # self.driver.find_element_by_xpath('/div[@id="nav-user-account"]/div[1]/div[1]/span[1]/a[1]').click() # element = self.driver.find_element_by_xpath('//div[@id="nav-user-account"]/div[1]') # print(element) # actions = ActionChains(self.driver) # actions.move_to_element(element) # actions.perform() # time.sleep(1) # WebDriverWait(self.driver, 40).until( # EC.element_to_be_clickable((By.XPATH, '//div[@id="nav-user-account"]/div[2]/div[2]/p[2]/a')) # ) WebDriverWait(self.driver, 40).until( EC.element_to_be_clickable((By.XPATH, '//span[@class="register-btn"]/a')) ) #点击登录 self.driver.find_element_by_xpath('//span[@class="register-btn"]/a').click() # WebDriverWait(self.driver, 40).until( # EC.frame_to_be_available_and_switch_to_it # ) time.sleep(3) #切换到子iframe self.driver.switch_to.frame('alibaba-login-box') #输入登录信息 WebDriverWait(self.driver, 40).until( EC.element_to_be_clickable((By.XPATH, '//input[@id="fm-login-id"]')) ) self.driver.find_element_by_id('fm-login-id').send_keys(item[1]) self.driver.find_element_by_id('fm-login-password').send_keys(item[2]) self.driver.find_element_by_id('fm-login-submit').click() #弹窗处理 try: WebDriverWait(self.driver, 40).until( EC.element_to_be_clickable((By.CLASS_NAME, 'close-layer')) ) self.driver.find_element_by_class_name('close-layer').click() except Exception: pass WebDriverWait(self.driver,40).until( EC.element_to_be_clickable((By.ID,"search-key")) ) self.driver.find_element_by_id('search-key').send_keys(self.keyword) self.driver.find_element_by_class_name('search-button').send_keys(Keys.ENTER) for i in range(1,6): source = self.driver.page_source WebDriverWait(driver=self.driver, timeout=30).until( EC.element_to_be_clickable((By.XPATH, '//span[@class="ui-pagination-active"]/following-sibling::a[1]')) ) result = self.parse_list_page(source) if result: self.get_detail_page_to_add(self.target_good_detail_url) self.add_other_goods() self.delete_other_goods_from_cart() self.place_order() break else: next_page_btn = self.driver.find_element_by_xpath('//span[@class="ui-pagination-active"]/following-sibling::a[1]') next_page_btn.click() # print('第%d列表搜索页没找到目标商品,正在跳转到第%d列表搜索页'% (i,i+1)) self.logger.info('第%d列表搜索页没找到目标商品,正在跳转到第%d列表搜索页'% (i,i+1)) time.sleep(3)
def verifyLogo(): # wait until logo on the page is visible and verify its text logo = WebDriverWait(driver, 30).until( EC.visibility_of_element_located((By.CLASS_NAME, "tn-atom"))) assert logo.text == "DATAFOLKS" driver = webdriver.Firefox(executable_path=GeckoDriverManager().install()) driver.get("https://www.datafolks.com") verifyLogo() # wait until Contact Us Button in Navigation bar is clickable and Click on "Contact Us" button contact_us_home_btn = WebDriverWait(driver, 40).until( EC.element_to_be_clickable((By.XPATH, "//div[13]/a"))) contact_us_home_btn.click() #Fill in input field for name name_input_field = WebDriverWait(driver, 40).until( (EC.visibility_of_element_located((By.NAME, "Name")))) name_input_field.send_keys("Josh Traverson") #Fill in input field for email name_input_field = WebDriverWait(driver, 40).until( (EC.visibility_of_element_located((By.NAME, "Email")))) name_input_field.send_keys("*****@*****.**") #Fill in input field for name phone number phone_input_field = WebDriverWait(driver, 40).until( (EC.visibility_of_element_located((By.NAME, "Phone"))))
def register(self): self.driver.get('https://discord.com/register') free_print(f"{Fore.LIGHTMAGENTA_EX}[!]{Style.RESET_ALL} Webdriver wait") WebDriverWait(self.driver, 1).until(EC.presence_of_element_located((By.XPATH, "//input[@type='email']"))) free_print(f"{Fore.LIGHTMAGENTA_EX}[*]{Style.RESET_ALL} " +self.email) self.driver.find_element_by_xpath("//input[@type='email']").send_keys(self.email) free_print(f"{Fore.LIGHTMAGENTA_EX}[*]{Style.RESET_ALL} " +self.username) self.driver.find_element_by_xpath("//input[@type='text']").send_keys(self.username) free_print(f"{Fore.LIGHTMAGENTA_EX}[*]{Style.RESET_ALL} " +self.password) self.driver.find_element_by_xpath("//input[@type='password']").send_keys(self.password) free_print(f"{Fore.LIGHTMAGENTA_EX}[*]{Style.RESET_ALL}" +' Random Date') dateWorking = False #sometimes different discord languages have different xpath locations try: #if date could not be found via divs self.driver.find_element_by_xpath('//*[@id="app-mount"]/div[2]/div/div[2]/div/form/div/div[2]/div[4]/div[1]/div[1]/div/div/div/div/div[2]/div').click() dateWorking = True except: free_print(f"{Fore.LIGHTMAGENTA_EX}[!]{Style.RESET_ALL} " + 'Error in typing date. Please type the date manually.') input(f"{Fore.LIGHTMAGENTA_EX}[!]{Style.RESET_ALL} Submit your form manually. Have you solved captcha? [y/n] > ") dateWorking = False if dateWorking: actions = ActionChains(self.driver) actions.send_keys(str(random.randint(1,12)))# Month actions.send_keys(Keys.ENTER) actions.send_keys(str(random.randint(1,28))) #Day actions.send_keys(Keys.ENTER) random_year = [1989,1990,1991,1992,1993,1994,1995,1996,1997,1998,1999,2000] actions.send_keys(str(random.choice(random_year))) #Year actions.perform() #Submit form try: self.driver.find_element_by_class_name('inputDefault-3JxKJ2').click() # Agree to terms and conditions except: free_print(f"{Fore.LIGHTMAGENTA_EX}[*]{Style.RESET_ALL} Could not find button. Ignoring..") pass #input(f'{Fore.LIGHTMAGENTA_EX}[!]{Style.RESET_ALL} Press ENTER to create account.') self.driver.find_element_by_class_name('button-3k0cO7').click() # Submit button free_print(f'{Fore.LIGHTMAGENTA_EX}[*]{Style.RESET_ALL} Submit form') while True: lock.acquire() checker = input(f"{Fore.LIGHTMAGENTA_EX}[!]{Style.RESET_ALL} Have you solved the captcha and submit? [y/n] > ") lock.release() if checker == "y": break return True elif checker =="n": sys.exit() return False
def place_order(self): #页面刷新,可有可无 self.driver.refresh() WebDriverWait(driver=self.driver, timeout=20).until( EC.element_to_be_clickable((By.XPATH, '//div[@class="product-price-info3"]/a[1]')) ) quantity = self.driver.find_element_by_xpath('//input[@readonly="readonly"]').get_attribute('value') if quantity == '1': price_text = self.driver.find_element_by_xpath('//span[@class="total-price ui-cost notranslate"]/b').text price = re.sub(r'US \$','',price_text) # print('订单总价:',price) self.logger.info('订单总价:%s' % price) self.driver.find_element_by_xpath('//div[@class="product-price-info3"]/a[1]').click() #可能先跳转到用户登录的ifame,但登录成功后需要进行刮图验证码和手机验证码的双重验证,暂无有效识别方法。具体原因可能是同一IP注册用户数量的过多,导致的验证。 # try: # time.sleep(3) # self.driver.switch_to.frame('alibaba-login-box') # #输入登录信息 # WebDriverWait(self.driver, 40).until( # EC.element_to_be_clickable((By.XPATH, '//input[@id="fm-login-id"]')) # ) # self.driver.find_element_by_id('fm-login-id').send_keys(item[1]) # self.driver.find_element_by_id('fm-login-password').send_keys(item[2]) # self.driver.find_element_by_id('fm-login-submit').click() # except Exception: # pass # 跳转到个人信息填写页面 WebDriverWait(driver=self.driver, timeout=30).until( EC.element_to_be_clickable((By.XPATH, '//div[@class="sa-btn-group"]/a')) ) self.driver.find_element_by_name('contactPerson').send_keys('xiaoqiang') country_selectBtn = Select(self.driver.find_element_by_name('country')) country_selectBtn.select_by_visible_text('United States') self.driver.find_element_by_name('address').send_keys('42TH STREET') self.driver.find_element_by_name('address2').send_keys('910 1FL') state_selectBtn = Select( self.driver.find_element_by_xpath('//input[@name="province"]/following-sibling::select[1]')) state_selectBtn.select_by_visible_text('New York') # 此处需要时间延迟,来加载城市的下拉列表 time.sleep(2) city_selectBtn = Select( self.driver.find_element_by_xpath('//input[@name="city"]/following-sibling::select[1]')) city_selectBtn.select_by_visible_text('Brooklyn') self.driver.find_element_by_name('zip').send_keys('11219') self.driver.find_element_by_name('mobileNo').send_keys('13729005200') self.driver.find_element_by_name('isDefault').click() self.driver.find_element_by_xpath('//div[@class="sa-btn-group"]/a[1]').click() # 填写支付信息 WebDriverWait(driver=self.driver, timeout=30).until( EC.element_to_be_clickable((By.XPATH, '//input[@name="cardNum"]')) ) self.driver.find_element_by_name('cardNum').send_keys('5329598063750158') self.driver.find_element_by_name('dateM').send_keys('03') self.driver.find_element_by_name('dateY').send_keys('21') self.driver.find_element_by_name('cvv2').send_keys('140') self.driver.find_element_by_name('cardHolderF').send_keys('xiaoqiang') self.driver.find_element_by_name('cardHolderL').send_keys('huang') self.driver.find_element_by_name('saveinfo').click() self.driver.find_element_by_xpath('//div[@class="payment-line"]/button[1]').click() WebDriverWait(driver=self.driver, timeout=30).until( EC.element_to_be_clickable((By.XPATH, '//div[@class="place-order-button"]/button[1]')) ) self.driver.find_element_by_xpath('//div[@class="place-order-button"]/button[1]').click() # print('下单成功!') self.logger.info('下单成功!') time.sleep(2) self.driver.back() time.sleep(2) self.driver.execute_script('window.scrollTo(document.body.scrollWidth,0 );') WebDriverWait(driver=self.driver, timeout=30).until( EC.presence_of_element_located((By.XPATH, '//div[@id="nav-user-account"]/div[1]')) ) element = self.driver.find_element_by_xpath('//div[@id="nav-user-account"]/div[1]') # print(element) actions = ActionChains(self.driver) actions.move_to_element(element) actions.perform() time.sleep(1) WebDriverWait(self.driver, 40).until( EC.element_to_be_clickable((By.XPATH, '//ul[@class="flyout-quick-entry"]/li[2]/a')) ) self.driver.find_element_by_xpath('//ul[@class="flyout-quick-entry"]/li[2]/a').click() time.sleep(2) orderId = self.driver.find_element_by_xpath('//*[@id="buyer-ordertable"]//tr[1]/td[2]/p[1]/span[2]').text # print("订单号为:",orderId) self.logger.info("订单号为:%s" % orderId)
def findElement(by, value): element = WebDriverWait(driver, 10, 1).until(lambda x: x.find_element(by, value)) return element
with open(DIRNAME + "/" + fname, "w+") as f: json.dump(obj, f) #driver = webdriver.Firefox() # phantomjs_path = "C:\coding\JeevesCoursePlanner\scraper\phantomjs.exe" #driver = webdriver.PhantomJS(executable_path=phantomjs_path, service_log_path=os.path.devnull) #driver.set_window_size(1400, 1000) driver = webdriver.Chrome() driver.get("http://albert.nyu.edu/course-finder") driver.select = driver.find_element_by_css_selector # too wordy timeout = 30 coursesearchlink = WebDriverWait(driver, timeout).until( EC.presence_of_element_located((By.PARTIAL_LINK_TEXT, "Course Search"))) coursesearchlink.click() # driver.switch_to_frame("TargetContent") # We are now in the albert course search. checkbox = WebDriverWait(driver, timeout).until( EC.presence_of_element_located( (By.CSS_SELECTOR, "#NYU_CLS_WRK_NYU_SPRING"))) # selects the semester checkbox.click() time.sleep(DELAY) """ { "ART-SHU": { "2": { "name": "Introduction to Studio Art - Chinese Traditional Methods in Contemporary Art",
# NAME = "name" # TAG_NAME = "tag name" # CLASS_NAME = "class name" # CSS_SELECTOR = "css selector" # driver.find_element(By.ID, "kw") # 另外一种写法——参数化定位 driver.find_element("id", "kw") driver.find_element("xpath", "//*[@id='kw']") driver.find_element("css selector", "#kw") driver.find_element("class name", "xxx") driver.find_element("tag name", "xxxxxx") element = WebDriverWait(driver, 10, 1).until(lambda x: x.find_element_by_id( "kw")) # 找到了就返回元素,10s之后找不到就抛异常:TimeoutException element.send_keys("hehe") element1 = WebDriverWait(driver, 10, 1).until(lambda x: x.find_element_by_id("su")) element.click() def element_is_disappeared(self, locator, timeout=30): is_disappeared = WebDriverWait( self.driver, timeout, 1).until_not(lambda x: x.find_element(*locator).is_displayed()) return is_disappeared def findElement(by, value):
def element_is_disappeared(self, locator, timeout=30): is_disappeared = WebDriverWait( self.driver, timeout, 1).until_not(lambda x: x.find_element(*locator).is_displayed()) return is_disappeared
from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.common.exceptions import TimeoutException from pyquery import PyQuery as pq from pymongo import MongoClient import re client = MongoClient() db = client.taobao data = db.data chromedriver = 'C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe' browser = webdriver.Chrome(chromedriver) wait = WebDriverWait(browser, 10) #进入淘宝网,输入鞋子,返回页面 def search(kd): try: browser.get('https://www.taobao.com/') input = wait.until( EC.presence_of_element_located((By.CSS_SELECTOR, '#q'))) submit = wait.until( EC.element_to_be_clickable( (By.CSS_SELECTOR, '#J_TSearchForm > div.search-button > button'))) input.send_keys(kd) submit.click() total = wait.until(
driver = webdriver.PhantomJS('/Users/dannihong/Downloads/phantomjs-2.1.1-macosx/bin/phantomjs') driver.set_window_size(1280, 2400) url = 'https://movie.douban.com/' driver.get(url) from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait import selenium.webdriver.support.expected_conditions as EC from selenium.webdriver.common.keys import Keys # 使用WebDriver指定:用哪个driver,等待多久时长;使用EC指定:等到什么样的EC条件;使用By指定:查找网页元素的方法 try: element = WebDriverWait(driver, 10).until( # 10秒为超时时长,超时抛异常,超时时长内在try里默认0.5秒循环执行 # 方式1:By.ID(通过id查找输入框元素) EC.presence_of_element_located((By.ID, "inp-query")) # 这里要传入的是元组 # 方式2:By.XPATH # EC.presence_of_element_located((By.XPATH, "//input[@id='inp-query']")) ) # 默认0.5秒循环一次,查看条件是否达到 # 模拟在输入框输入'我和我的祖国',然后回车键开始搜索 element.send_keys('我和我的祖国') element.send_keys(Keys.ENTER) print(driver.current_url) # 跳转到搜索后的url savepic() # 截图查看 except Exception as e: print(e) finally: driver.quit()
def _wait(self, wait_until=None, timeout=5): return WebDriverWait(self._driver, timeout).until(wait_until)
# Type in # -------------------------------------------------------------------------------------# from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC driver = webdriver.Chrome("C:\\Users\\Sean\\chromedriver.exe") driver.set_page_load_timeout(30) driver.get("https://crl.ptopenlab.com:8800/poem/sub?3") driver.implicitly_wait(20) driver.find_element_by_id("topictb").send_keys("发现") driver.find_element_by_id("startButton").click() wait = WebDriverWait(driver, 10) wait.until(EC.element_to_be_clickable((By.ID, 'title'))) url = driver.current_url driver.quit() # -------------------------------------------------------------------------------------# # Get character # -------------------------------------------------------------------------------------# import urllib.request res = urllib.request.urlopen(url).read() from bs4 import BeautifulSoup soup = BeautifulSoup(res, from_encoding="RTF-8")
def do(browser_obj, all): pictures = all.find_elements_by_xpath('./div/div') comment_counter = 0 for pic in pictures: pic.click() # TRY IF YOU GET A VIDEO OR ELSE DOWNLOAD THE IMAGE. try: src = WebDriverWait(b, 2).until( EC.presence_of_element_located( (By.XPATH, '//article/div/div/div/div/div//video'))) src = src.get_attribute('src') urllib.request.urlretrieve( src, os.getcwd() + '/' + folder_name + '/' + src.split('/')[-1]) except: # FOUND TWO XPATHS FOR IMAGES. INSTAGRAM RANDOMLY PLACES PICTURE IN ONE OF THEM. try: src = WebDriverWait(b, 2).until( EC.presence_of_element_located( (By.XPATH, '//article/div/div/div/div/img'))) except: src = WebDriverWait(b, 2).until( EC.presence_of_element_located( (By.XPATH, '//article/div/div/div/div/div/img'))) src = src.get_attribute('src') urllib.urlretrieve( src, os.getcwd() + '/' + folder_name + '/' + src.split('/')[-1]) finally: pass try: liked = browser_obj.find_element_by_xpath( '//article/div[2]/section[1]/a[1]/span[contains(text(), "Like")]' ) liked.click() except: pass ''' TO COMMENT ON PICS. WORKING BUT INSTA BLOCKS COMMENTS AFTER 5 PICS @TODO: ---> Comment on pics only after some interval of time. ''' if comment_counter == 5: text = browser_obj.find_element_by_xpath('//form/textarea') comment = random.choice(comment_list[0]) for i in range(1, len(comment_list)): comment += ' ' + random.choice(comment_list[i]) # REPLACE 'YOUR COMMENT' ON THE NEXT LINE WITH WHAT YOU WANT TO COMMENT # comment = input("Enter what do you want to comment: ") text.send_keys(comment + Keys.RETURN) comment_counter = 0 comment_counter += 1 ''' TO CLOSE THE IMAGE ''' cross = browser_obj.find_element_by_xpath( '//body//div/button[contains(text(), "Close")]') cross.click()
def testThatWeShouldNotAutoSwitchFocusToAnIFrameWhenAPageContainingThemIsLoaded( self): iframe_timeout = 3 wait = WebDriverWait(self.driver, iframe_timeout) self._loadPage("iframes") wait.until(lambda dr: dr.find_element_by_id("iframe_page_heading"))
option = webdriver.ChromeOptions() # 设置为开发者模式,避免被识别 option.add_experimental_option('excludeSwitches', ['enable-automation']) driver = webdriver.Chrome(options=option) driver.maximize_window() data = xlrd.open_workbook('flask.xlsx') table = data.sheets()[0] links = table.col_values(int(1)) dts = [] for url in links[1:]: try: driver.get(url) except Exception as e: print(e) wait = WebDriverWait(driver, 10, 0.2) # 设置等待时间 while True: # 评论区是异步加载的,需要向下滑动才会出现数据 driver.execute_script( "window.scrollTo(0, document.body.scrollHeight);") try: # 断言判断 网页源码是否有 ’没有更多评论‘ 这几个字 最底部才有,可以作为判断依据 assert '没有更多评论' in driver.page_source break except AssertionError as e: # 找不到就继续向下滑动 driver.execute_script( "window.scrollTo(0, document.body.scrollHeight);") try: # 查找评论区用户列表超链接 a_list = wait.until(EC.presence_of_all_elements_located( (By.XPATH, '//*[@class="user-face"]/a')),
def download(path, voice=voice, speed=speed, prosody=prosody): file = f'{path}/input.txt' content = open(file, 'r', encoding='utf-8').read() type = get_type(voice) if type == 'fpt': wraptexts = wrap(content, 480) else: wraptexts = wrap(content, 4999) count = 0 for i in range(len(wraptexts)): text = wraptexts[i] if os.path.exists("{}{:03}.mp3".format(path, i)): continue while True: try: if count >= 50: return False time.sleep(1) if type == 'fpt': api_key = random.choice(keys) print('\n', api_key) url = "http://api.openfpt.vn/text2speech/v4?api_key={}&voice={}&speed={}&prosody={}".format(api_key, voice, speed, prosody) # print(voice) response = requests.post(url, data=text.encode('utf-8'), headers={'voice':voice, 'speed':speed, 'prosody':prosody}) response = response.json() print('\n', response['async']) file = response['async'] else: driver = webdriver.Chrome() driver.get("https://vbee.vn") textarea = driver.find_element_by_class_name("ant-input") textarea.clear() textarea.send_keys(text) driver.find_element(By.XPATH, '//*[@id="root"]/div/div/div/div/div/div/div[2]/div[2]/div[1]/div/div/div').click() driver.find_element(By.XPATH, f'//li[contains(text(),"{voice}")]').click() button = driver.find_element_by_class_name("ant-btn") button.click() xp = '//*[@download]' element = WebDriverWait(driver, 500000).until(EC.presence_of_element_located((By.XPATH, xp))) file = element.get_attribute('href') count2 = 0 while True: if count2 >= 300: break try: print("downloading file {}/{} ".format(i+1, len(wraptexts)), "{}\{:03}.mp3".format(path, i)) wget.download(file, "{}/{:03}.mp3".format(path, i)) if type != 'fpt': song = AudioSegment.from_mp3("{}/{:03}.mp3".format(path, i)) extract = song[startTime:endTime] # Saving extract.export("{}/{:03}.mp3".format(path, i), format="mp3") count2 = 0 count = 0 break except: count2 += 1 time.sleep(1) if count2 == 0: break except : print('Waiting...', end='') count += 1 continue print('\nCOMPLETE') return True
driver = webdriver.Chrome(executable_path=os.path.abspath("chromedriver"),options=chrome_options) driver.get("https://www.hunter-ed.com/accounts/sign_in/") #logs into hunter safety website try: driver.find_element_by_id("account-username").send_keys('YOUR USERNAME HERE') driver.find_element_by_id("account-password").send_keys('YOUR PASSWORD HERE') driver.find_element_by_class_name("btn-lg").click() except: print('login failed. Exiting.') exit() try: cont = WebDriverWait(driver, 8).until( EC.element_to_be_clickable((By.CLASS_NAME, "btn-success"))) cont.click() except: print('continue where you left off FAILED. Exiting') exit() #click the next page button whenever it appears, and fails intentionally if nothing pops up after 200 seconds try: while(1): next_course = WebDriverWait(driver, 200).until( EC.element_to_be_clickable((By.CLASS_NAME, "btn-success"))) next_course.click() except: print('form already submitted, or landing page did not load') exit()
else: login_from_insta(browser_obj, username, password) # Creating a delay for log in to happen properly time.sleep(5) ''' GOING TO THE PROFILE OF THE SPECIFIED USER ''' browser_obj.get('http://instagram.com/' + friend_username + '/') ''' LOADING MORE PICTURES IN THEIR PROFILE ''' try: load_more = WebDriverWait(browser_obj, 10).until( EC.presence_of_element_located( (By.XPATH, '//a[contains(text(), "Load more")]'))) load_more.click() except: pass last_height = browser_obj.execute_script("return document.body.scrollHeight") while True: browser_obj.execute_script( "window.scrollTo(0, document.body.scrollHeight);") time.sleep(3) new_height = browser_obj.execute_script( "return document.body.scrollHeight") if new_height == last_height: break last_height = new_height