def testExpectedConditionElementSelectionStateToBe(self, driver, pages): pages.load("formPage.html") element = driver.find_element_by_id('checky') WebDriverWait(driver, 0.7).until(EC.element_selection_state_to_be(element, False)) assert element.is_selected() is False with pytest.raises(TimeoutException): WebDriverWait(driver, 0.7).until(EC.element_selection_state_to_be(element, True)) driver.execute_script("setTimeout(function(){document.getElementById('checky').checked = true}, 200)") WebDriverWait(driver, 0.7).until(EC.element_selection_state_to_be(element, True)) assert element.is_selected() is True
def testExpectedConditionElementSelectionStateToBe(self): self._loadPage("formPage") element = self.driver.find_element_by_id('checky') WebDriverWait(self.driver, 0.7).until(EC.element_selection_state_to_be(element, False)) self.assertFalse(element.is_selected()) try: WebDriverWait(self.driver, 0.7).until(EC.element_selection_state_to_be(element, True)) self.fail("Expected TimeoutException to have been thrown") except TimeoutException, e: pass
def testExpectedConditionElementSelectionStateToBe(driver, pages): pages.load("formPage.html") element = driver.find_element_by_id('checky') WebDriverWait(driver, 0.7).until(EC.element_selection_state_to_be(element, False)) assert element.is_selected() is False with pytest.raises(TimeoutException): WebDriverWait(driver, 0.7).until(EC.element_selection_state_to_be(element, True)) driver.execute_script("setTimeout(function(){document.getElementById('checky').checked = true}, 200)") WebDriverWait(driver, 0.7).until(EC.element_selection_state_to_be(element, True)) assert element.is_selected() is True
def testExpectedConditionElementSelectionStateToBe(self, driver, pages): if driver.capabilities['browserName'] == 'firefox' and driver.w3c: pytest.xfail("Marionette issue: https://bugzilla.mozilla.org/show_bug.cgi?id=1297551") pages.load("formPage.html") element = driver.find_element_by_id('checky') WebDriverWait(driver, 0.7).until(EC.element_selection_state_to_be(element, False)) assert element.is_selected() is False with pytest.raises(TimeoutException): WebDriverWait(driver, 0.7).until(EC.element_selection_state_to_be(element, True)) driver.execute_script("setTimeout(function(){document.getElementById('checky').checked = true}, 200)") WebDriverWait(driver, 0.7).until(EC.element_selection_state_to_be(element, True)) assert element.is_selected() is True
def testExpectedConditionElementSelectionStateToBe(self): self._loadPage("formPage") element = self.driver.find_element_by_id('checky') WebDriverWait(self.driver, 0.7).until( EC.element_selection_state_to_be(element, False)) self.assertFalse(element.is_selected()) try: WebDriverWait(self.driver, 0.7).until( EC.element_selection_state_to_be(element, True)) self.fail("Expected TimeoutException to have been thrown") except TimeoutException, e: pass
def testExpectedConditionElementSelectionStateToBe(self): self._loadPage("formPage") element = self.driver.find_element_by_id('checky') WebDriverWait(self.driver, 0.7).until(EC.element_selection_state_to_be(element, False)) self.assertFalse(element.is_selected()) try: WebDriverWait(self.driver, 0.7).until(EC.element_selection_state_to_be(element, True)) self.fail("Expected TimeoutException to have been thrown") except TimeoutException as e: pass self.driver.execute_script("setTimeout(function(){document.getElementById('checky').checked = true}, 200)") WebDriverWait(self.driver, 0.7).until(EC.element_selection_state_to_be(element, True)) self.assertTrue(element.is_selected())
def is_selected_be(self, locator, selected=True): """判断某个元素的选中状态是否符合预期,selected是期望的参数True/False,返回布尔值""" try: return self.according_wait(EC.element_selection_state_to_be(locator, selected)) except TimeoutException: self.log.error('元素没有定位到:%s' % str(locator)) return False
def element_selection_state_to_be(self, element, is_selected, driver=None, timeout=30): ''' An expectation for checking if the given element is selected. element is WebElement object is_selected is a Boolean.” @param: (by, arg) -- locator locator is tuple, as follow: (By.CLASS_NAME, class name) (By.CSS_SELECTOR, css selector) (By.ID, id) (By.LINK_TEXT, link text) (By.NAME, name) (By.PARTIAL_LINK_TEXT, partial link text) (By.TAG_NAME, tag name) (By.XPATH, xpath) ''' driver = driver or self.driver try: element = WebDriverWait(driver, timeout).until( EC.element_selection_state_to_be(element, is_selected)) return element except Exception as e: # print("wait_for_element timeout: ") print(str(e)) return None
def element_selection_state_to_be(locatortype, locator): try: wait = WebDriverWait(BaseFixture.driver, 10) element = BaseFixture.get_webelement(locatortype, locator) wait.until(EC.element_selection_state_to_be(element, True)) except: print(traceback.print_exc())
def web_until_not_element_selection_state_to_be(self, locator, is_selected): """判定元素选中状态不是与预期一致""" return WebDriverWait(self.driver, self.timeout, self.poll_frequency, self.ignored_exceptions).until_not( EC.element_selection_state_to_be( locator, is_selected))
def fill_form(self, form_data): """ Fill in the form with the given data. """ for field, value in form_data.items(): element = self.form.find_element_by_name(field) if element.get_attribute('type') == 'checkbox': if bool(value) != element.is_selected(): element.click() # Before moving on, make sure checkbox state (checked/unchecked) corresponds to desired value WebDriverWait(self.client, timeout=5) \ .until(expected_conditions.element_selection_state_to_be(element, value)) continue if element.get_attribute('type') == 'color': # Selenium doesn't support typing into HTML5 color field with send_keys id_elem = element.get_attribute('id') self.client.execute_script( "document.getElementById('{}').type='text'".format( id_elem)) if not element.get_attribute( 'readonly') and not element.get_attribute( 'type') == 'hidden': element.clear() element.send_keys(value) # Before moving on, make sure input field contains desired text WebDriverWait(self.client, timeout=5) \ .until(expected_conditions.text_to_be_present_in_element_value((By.NAME, field), value))
def _check_element_enable(self, *selector): try: return WebDriverWait(self.driver, TIMEOUT).until( EC.element_selection_state_to_be( self.driver.find_element(*selector), True)) except (NoSuchElementException, ElementNotVisibleException, TimeoutException): return False
def wait(self, locator, value="click"): if value == "visable": WebDriverWait(self._driver, 5).until( expected_conditions.visibility_of_element_located(locator)) elif value == "selected": WebDriverWait(self._driver, 5).until( expected_conditions.element_selection_state_to_be(locator)) else: WebDriverWait(self._driver, 5).until( expected_conditions.element_to_be_clickable(locator))
def testExpectedConditionElementSelectionStateToBe(self, driver, pages): if driver.capabilities['browserName'] == 'firefox' and driver.w3c: pytest.xfail( "Marionette issue: https://bugzilla.mozilla.org/show_bug.cgi?id=1297551" ) pages.load("formPage.html") element = driver.find_element_by_id('checky') WebDriverWait(driver, 0.7).until( EC.element_selection_state_to_be(element, False)) assert element.is_selected() is False with pytest.raises(TimeoutException): WebDriverWait(driver, 0.7).until( EC.element_selection_state_to_be(element, True)) driver.execute_script( "setTimeout(function(){document.getElementById('checky').checked = true}, 200)" ) WebDriverWait(driver, 0.7).until( EC.element_selection_state_to_be(element, True)) assert element.is_selected() is True
def check_element_selection_state_to_be(self, *element, **kwargs): timeout=kwargs.get('timeout', self.default_delay) query=None try: query = WebDriverWait(self.driver, timeout).until( EC.element_selection_state_to_be(element)) except: print('check_element_selection_state_to_be(%s, %s): timeout to find element\n' % element) finally: return query
def element_selection_state_to_be(self, element, state, **kwargs): """ An expectation for checking if the given element is selected. element is WebElement object Arguments element (obj): element to check for state (bool): true or false timeout (int): seconds to wait for message (str): message to display if timed out kwargs (dict): any other argument for WebDriverWait() api """ condition = EC.element_selection_state_to_be(element, state) return self(condition, **kwargs)
def is_selected_to_be(self, locator, timeout=global_timeout, selected=True): ''' 判断元素的选中状态 :param locator: 定位信息 :param timeout: 超时时间 :param selected: 默认True :return: ''' return WebDriverWait(self.driver, timeout, 1).until( EC.element_selection_state_to_be(locator))
def wait_for_condition(self, locator, condition): """ waiting for element expect condition Args: locator (str): css selector condition (bool): expect condition (False: unselected, True: selected) Return: bool """ try: elm = self.driver.find_element_by_css_selector(locator) res = WebDriverWait(self.driver, DEFAULT_WAIT_TIME).until( EC.element_selection_state_to_be(elm, condition)) return res except NoSuchElementException: self.driver.save_screenshot('wait_for_condition_failed.png') return False
def check_element_selection_state_to_be(self, *element, **kwargs): """To check the element selection state on the page. :param element: id or name :type element: string :param kwargs: element :type kwargs: string :return: Web driver query output """ timeout = kwargs.get("timeout", self.default_delay) query = None try: query = WebDriverWait(self.driver, timeout).until( EC.element_selection_state_to_be(element)) except Exception: print("check_element_selection_state_to_be({}): timeout to find " "element".format(element)) return query
def get_element_selection_state_to_be(self, element, state=True, timeout=10): try: log.info("查看列表勾选结果和预期结果对比") result = WebDriverWait(self.driver, timeout=timeout, poll_frequency=1, ignored_exceptions=None).until( EC.element_selection_state_to_be( element, state)) except Exception as msg: log.error("勾选结果和预期结果不匹配%s" % msg) return False else: log.info("确认符合预期") return result
def fill_form(self, form_data): """ Fill in the form with the given data. """ for field, value in form_data.items(): element = self.form.find_element_by_name(field) if element.get_attribute('type') == 'checkbox': if bool(value) != element.is_selected(): element.click() # Before moving on, make sure checkbox state (checked/unchecked) corresponds to desired value WebDriverWait(self.client, timeout=5) \ .until(expected_conditions.element_selection_state_to_be(element, value)) elif not element.get_attribute('readonly') and not element.get_attribute('type') == 'hidden': element.clear() element.send_keys(value) # Before moving on, make sure input field contains desired text WebDriverWait(self.client, timeout=5) \ .until(expected_conditions.text_to_be_present_in_element_value((By.NAME, field), value))
def check_element_selection_state_to_be(self, *element, **kwargs): """To check the element selection state on the page :param element: id or name :type element: string :param kwargs: element :type kwargs: string :return: Web driver query output """ timeout = kwargs.get('timeout', self.default_delay) query = None try: query = WebDriverWait(self.driver, timeout).until( EC.element_selection_state_to_be(element)) except: print( 'check_element_selection_state_to_be(%s, %s): timeout to find element\n' % element) return query
def fill_form(self, form_data): """ Fill in the form with the given data. """ for field, value in form_data.items(): element = self.form.find_element_by_name(field) if element.get_attribute('type') == 'checkbox': if bool(value) != element.is_selected(): element.click() # Before moving on, make sure checkbox state (checked/unchecked) corresponds to desired value WebDriverWait(self.client, timeout=5) \ .until(expected_conditions.element_selection_state_to_be(element, value)) elif not element.get_attribute( 'readonly') and not element.get_attribute( 'type') == 'hidden': element.clear() element.send_keys(value) # Before moving on, make sure input field contains desired text WebDriverWait(self.client, timeout=5) \ .until(expected_conditions.text_to_be_present_in_element_value((By.NAME, field), value))
def fill_form(self, form_data, validate_fields=None): """ Fill in the form with the given data. """ validate_fields = validate_fields or () for field, value in form_data.items(): element = self.form.find_element_by_name(field) if element.get_attribute('type') == 'checkbox': if bool(value) != element.is_selected(): element.click() # Before moving on, make sure checkbox state (checked/unchecked) corresponds to desired value WebDriverWait(self.client, timeout=5) \ .until(expected_conditions.element_selection_state_to_be(element, value)) continue if element.get_attribute('type') == 'color': # Selenium doesn't support typing into HTML5 color field with send_keys id_elem = element.get_attribute('id') self.client.execute_script( "document.getElementById('{}').type='text'".format( id_elem)) if not element.get_attribute( 'readonly') and not element.get_attribute( 'type') == 'hidden': element.clear() if value: # A small delay is required for angular to properly mark field as dirty element.click() time.sleep(.5) element.send_keys(value) # Before moving on, make sure input field contains desired text WebDriverWait(self.client, timeout=5) \ .until(expected_conditions.text_to_be_present_in_element_value((By.NAME, field), value)) # And that the server validation, if any, has completed if field in validate_fields: WebDriverWait(self.client, timeout=10) \ .until(ServerValidationComplete((By.NAME, field)))
def wait_until (self, by, path, *args): """Wait until expected condition is met. Used internal to find method""" try: wait = WebDriverWait(self.driver, 10) if self.config["until"] == "click": _ = wait.until(EC.element_to_be_clickable((by, path))) if self.config["until"] == "present": _ = wait.until(EC.presence_of_element_located((by, path))) if self.config["until"] == "visible": _ = wait.until(EC.visibility_of_element_located((by, path))) if self.config["until"] == "invisible": _ = wait.until(EC.invisibility_of_element_located((by, path))) if self.config["until"] == "text_present": _ = wait.until(EC.text_to_be_present_in_element((by, path, kwargs))) #_text if self.config["until"] == "text_present_locator": _ = wait.until(EC.text_to_be_present_in_element_value((by, path, kwargs))) #_text if self.config["until"] == "selection_state": _ = wait.until(EC.element_selection_state_to_be((by, path))) if self.config["until"] == "located_selection_state": _ = wait.until(EC.element_located_selection_state_to_be((by, path, kwargs))) #is_selected return True except: return False
EC.element_to_be_clickable((By.XPATH, "//*[@id='u1']/a[8]"))).click() '''判断某个元素中是否可见并且是enable的,代表可点击''' driver.find_element_by_xpath("//*[@id='wrapper']/div[6]/a[1]").click() #WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//*[@id='wrapper']/div[6]/a[1]"))).click() #WebDriverWait(driver,10).until(EC.staleness_of(driver.find_element(By.ID,'su'))) '''等待某个元素从dom树中移除''' #这里没有找到合适的例子 WebDriverWait(driver, 10).until( EC.element_to_be_selected( driver.find_element(By.XPATH, "//*[@id='nr']/option[1]"))) '''判断某个元素是否被选中了,一般用在下拉列表''' WebDriverWait(driver, 10).until( EC.element_selection_state_to_be( driver.find_element(By.XPATH, "//*[@id='nr']/option[1]"), True)) '''判断某个元素的选中状态是否符合预期''' WebDriverWait(driver, 10).until( EC.element_located_selection_state_to_be( (By.XPATH, "//*[@id='nr']/option[1]"), True)) '''判断某个元素的选中状态是否符合预期''' driver.find_element_by_xpath(".//*[@id='gxszButton']/a[1]").click() instance = WebDriverWait(driver, 10).until(EC.alert_is_present()) '''判断页面上是否存在alert,如果有就切换`到alert并返回alert的内容''' print(instance.text) instance.accept() driver.close()
self.driver.execute_script("setTimeout(function(){document.getElementById('checky').checked = true}, 200)") WebDriverWait(self.driver, 0.7).until(EC.element_located_to_be_selected((By.ID, 'checky'))) self.assertTrue(element.is_selected()) def testExpectedConditionElementSelectionStateToBe(self): self._loadPage("formPage") element = self.driver.find_element_by_id('checky') WebDriverWait(self.driver, 0.7).until(EC.element_selection_state_to_be(element, False)) self.assertFalse(element.is_selected()) try: WebDriverWait(self.driver, 0.7).until(EC.element_selection_state_to_be(element, True)) self.fail("Expected TimeoutException to have been thrown") except TimeoutException, e: pass self.driver.execute_script("setTimeout(function(){document.getElementById('checky').checked = true}, 200)") WebDriverWait(self.driver, 0.7).until(EC.element_selection_state_to_be(element, True)) self.assertTrue(element.is_selected()) def testExpectedConditionElementLocatedSelectionStateToBe(self): self._loadPage("formPage") element = self.driver.find_element_by_id('checky') WebDriverWait(self.driver, 0.7).until(EC.element_located_selection_state_to_be((By.ID, 'checky'), False)) self.assertFalse(element.is_selected()) try: WebDriverWait(self.driver, 0.7).until(EC.element_located_selection_state_to_be((By.ID, 'checky'), True)) self.fail("Expected TimeoutException to have been thrown") except TimeoutException, e: pass self.driver.execute_script("setTimeout(function(){document.getElementById('checky').checked = true}, 200)") WebDriverWait(self.driver, 0.7).until(EC.element_located_selection_state_to_be((By.ID, 'checky'), True)) self.assertTrue(element.is_selected())
def check_element_selection_state_to_be(self, element, is_selected): self.wait.until(EC.element_selection_state_to_be(element, is_selected))
sleep(3) driver.find_element_by_link_text("单程").click() sleep(3) radio = driver.find_element_by_id('sf2') radio.click() locators = (By.XPATH, "//input[@id='sf2']") try: #显性等待,判断radio是否被勾选 WebDriverWait(driver, 5).until( EC.element_located_selection_state_to_be(locators, True)) #判断勾选checkbox checkbox = driver.find_element_by_xpath("//input[@id='auto_query']") checkbox.click() # 显性等待,判断checkbox是否被勾选 WebDriverWait(driver, 5).until(EC.element_selection_state_to_be(checkbox, True)) # 出发地 fs = driver.find_element_by_xpath("//input[@id='fromStationText']") fs.clear() fs.send_keys('深圳') sleep(2) # 目的地 ts = driver.find_element_by_xpath("//input[@id='toStationText']") ts.clear() ts.send_keys('武汉') sleep(2) # 搜索按钮 search = driver.find_element_by_xpath("//*[@id='query_ticket']") # 构造对象 action_chains.move_to_element(search).perform() #search.click()
def wait_animation(self): WebDriverWait(self.driver, 10).\ until(EC.element_selection_state_to_be(self.driver.find_element(By.CSS_SELECTOR, Locator.on_cloud_page_button), False))
EC.text_to_be_present_in_element_value((By.CLASS_NAME, "b6e2")) ) element = WebDriverWait(driver, 10).until( EC.frame_to_be_available_and_switch_to_it((By.CLASS_NAME, "b6e2")) ) element = WebDriverWait(driver, 10).until( EC.invisibility_of_element_located((By.CLASS_NAME, "b6e2")) ) element = WebDriverWait(driver, 10).until( EC.element_to_be_clickable((By.CLASS_NAME, "b6e2")) ) element = WebDriverWait(driver, 10).until( EC.element_to_be_selected((By.CLASS_NAME, "b6e2")) )element = WebDriverWait(driver, 10).until( EC.element_located_to_be_selected((By.CLASS_NAME, "b6e2")) ) )element = WebDriverWait(driver, 10).until( EC.element_selection_state_to_be((By.CLASS_NAME, "b6e2")) ) )element = WebDriverWait(driver, 10).until( EC.element_located_selection_state_to_be((By.CLASS_NAME, "b6e2")) ) )element = WebDriverWait(driver, 10).until( EC.alert_is_present((By.CLASS_NAME, "b6e2")) )
def element_selection_state_to_be(self,selector): '''判断某个元素的选中状态是否符合预期''' return WebDriverWait(self.driver, 10).until( EC.element_selection_state_to_be(self.find_element(selector), True))
def is_selected_be(self, locator, selected=True): """判断某个元素的选中状态是否符合预期,selected是期望的参数True/False,返回布尔值""" return self.according_wait( EC.element_selection_state_to_be(locator, selected))
element = WebDriverWait(driver, 10).until(EC.title_is((By.ID, "myDynamicElement"))) element = WebDriverWait(driver, 10).until( EC.title_contains((By.ID, "myDynamicElement"))) element = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.ID, "myDynamicElement"))) element = WebDriverWait(driver, 10).until(EC.visibility_of( (By.ID, "myDynamicElement"))) element = WebDriverWait(driver, 10).until( EC.presence_of_all_elements_located((By.ID, "myDynamicElement"))) element = WebDriverWait(driver, 10).until( EC.text_to_be_present_in_element((By.ID, "myDynamicElement"))) element = WebDriverWait(driver, 10).until( EC.text_to_be_present_in_element_value((By.ID, "myDynamicElement"))) element = WebDriverWait(driver, 10).until( EC.frame_to_be_available_and_switch_to_it((By.ID, "myDynamicElement"))) element = WebDriverWait(driver, 10).until( EC.invisibility_of_element_located((By.ID, "myDynamicElement"))) element = WebDriverWait(driver, 10).until( EC.element_to_be_clickable((By.ID, "myDynamicElement"))) element = WebDriverWait(driver, 10).until( EC.element_to_be_selected((By.ID, "myDynamicElement"))) element = WebDriverWait(driver, 10).until( EC.element_located_to_be_selected((By.ID, "myDynamicElement"))) element = WebDriverWait(driver, 10).until( EC.element_selection_state_to_be((By.ID, "myDynamicElement"))) element = WebDriverWait(driver, 10).until( EC.element_located_selection_state_to_be((By.ID, "myDynamicElement"))) element = WebDriverWait(driver, 10).until( EC.alert_is_present((By.ID, "myDynamicElement")))
def is_selected_be(self, locator, selected=True): '''判断元素的状态,selected是期望的参数true/false''' result = WebDriverWait(self.driver, self.timeout, 1).until(EC.element_selection_state_to_be(locator, selected)) return result