Best Python code snippet using SeleniumBase
BaseCase.py
Source:BaseCase.py
...138 timeout=Settings.SMALL_TIMEOUT):139 if self.timeout_multiplier and timeout == Settings.SMALL_TIMEOUT:140 timeout = self._get_new_timeout(timeout)141 if self.browser == 'phantomjs':142 if self.is_partial_link_text_visible(partial_link_text):143 element = self.wait_for_partial_link_text(partial_link_text)144 element.click()145 return146 source = self.driver.page_source147 soup = BeautifulSoup(source)148 html_links = soup.fetch('a')149 for html_link in html_links:150 if partial_link_text in html_link.text:151 for html_attribute in html_link.attrs:152 if html_attribute[0] == 'href':153 href = html_attribute[1]154 if href.startswith('//'):155 link = "http:" + href156 elif href.startswith('/'):157 url = self.driver.current_url158 domain_url = self.get_domain_url(url)159 link = domain_url + href160 else:161 link = href162 self.open(link)163 return164 raise Exception(165 'Could not parse link from partial link_text '166 '[%s]' % partial_link_text)167 raise Exception(168 "Partial link text [%s] was not found!" % partial_link_text)169 # Not using phantomjs170 element = self.wait_for_partial_link_text(171 partial_link_text, timeout=timeout)172 pre_action_url = self.driver.current_url173 try:174 element.click()175 except StaleElementReferenceException:176 self.wait_for_ready_state_complete()177 time.sleep(0.05)178 element = self.wait_for_partial_link_text(179 partial_link_text, timeout=timeout)180 element.click()181 if Settings.WAIT_FOR_RSC_ON_CLICKS:182 self.wait_for_ready_state_complete()183 def get_text(self, selector, by=By.CSS_SELECTOR,184 timeout=Settings.SMALL_TIMEOUT):185 if self.timeout_multiplier and timeout == Settings.SMALL_TIMEOUT:186 timeout = self._get_new_timeout(timeout)187 self.wait_for_ready_state_complete()188 time.sleep(0.01)189 if PageUtils.is_xpath_selector(selector):190 by = By.XPATH191 element = PageActions.wait_for_element_visible(192 self.driver, selector, by, timeout)193 try:194 element_text = element.text195 except StaleElementReferenceException:196 self.wait_for_ready_state_complete()197 time.sleep(0.06)198 element = PageActions.wait_for_element_visible(199 self.driver, selector, by, timeout)200 element_text = element.text201 return element_text202 def get_attribute(self, selector, attribute, by=By.CSS_SELECTOR,203 timeout=Settings.SMALL_TIMEOUT):204 if self.timeout_multiplier and timeout == Settings.SMALL_TIMEOUT:205 timeout = self._get_new_timeout(timeout)206 self.wait_for_ready_state_complete()207 time.sleep(0.01)208 if PageUtils.is_xpath_selector(selector):209 by = By.XPATH210 element = PageActions.wait_for_element_present(211 self.driver, selector, by, timeout)212 try:213 attribute_value = element.get_attribute(attribute)214 except StaleElementReferenceException:215 self.wait_for_ready_state_complete()216 time.sleep(0.06)217 element = PageActions.wait_for_element_present(218 self.driver, selector, by, timeout)219 attribute_value = element.get_attribute(attribute)220 if attribute_value is not None:221 return attribute_value222 else:223 raise Exception("Element [%s] has no attribute [%s]!" % (224 selector, attribute))225 def refresh_page(self):226 self.driver.refresh()227 def get_current_url(self):228 return self.driver.current_url229 def get_page_source(self):230 return self.driver.page_source231 def get_page_title(self):232 return self.driver.title233 def go_back(self):234 self.driver.back()235 def go_forward(self):236 self.driver.forward()237 def close(self):238 self.driver.close()239 def get_image_url(self, selector, by=By.CSS_SELECTOR,240 timeout=Settings.SMALL_TIMEOUT):241 if self.timeout_multiplier and timeout == Settings.SMALL_TIMEOUT:242 timeout = self._get_new_timeout(timeout)243 return self.get_attribute(selector,244 attribute='src', by=by, timeout=timeout)245 def add_text(self, selector, new_value, by=By.CSS_SELECTOR,246 timeout=Settings.SMALL_TIMEOUT):247 if self.timeout_multiplier and timeout == Settings.SMALL_TIMEOUT:248 timeout = self._get_new_timeout(timeout)249 element = self.wait_for_element_visible(250 selector, by=by, timeout=timeout)251 pre_action_url = self.driver.current_url252 try:253 if not new_value.endswith('\n'):254 element.send_keys(new_value)255 else:256 new_value = new_value[:-1]257 element.send_keys(new_value)258 element.send_keys(Keys.RETURN)259 if Settings.WAIT_FOR_RSC_ON_PAGE_LOADS:260 self.wait_for_ready_state_complete()261 except StaleElementReferenceException:262 self.wait_for_ready_state_complete()263 time.sleep(0.06)264 element = self.wait_for_element_visible(265 selector, by=by, timeout=timeout)266 if not new_value.endswith('\n'):267 element.send_keys(new_value)268 else:269 new_value = new_value[:-1]270 element.send_keys(new_value)271 element.send_keys(Keys.RETURN)272 if Settings.WAIT_FOR_RSC_ON_PAGE_LOADS:273 self.wait_for_ready_state_complete()274 def send_keys(self, selector, new_value, by=By.CSS_SELECTOR,275 timeout=Settings.SMALL_TIMEOUT):276 if self.timeout_multiplier and timeout == Settings.SMALL_TIMEOUT:277 timeout = self._get_new_timeout(timeout)278 self.add_text(selector, new_value, by=by, timeout=timeout)279 def update_text_value(self, selector, new_value, by=By.CSS_SELECTOR,280 timeout=Settings.SMALL_TIMEOUT, retry=False):281 if self.timeout_multiplier and timeout == Settings.SMALL_TIMEOUT:282 timeout = self._get_new_timeout(timeout)283 element = self.wait_for_element_visible(284 selector, by=by, timeout=timeout)285 try:286 element.clear()287 except StaleElementReferenceException:288 self.wait_for_ready_state_complete()289 time.sleep(0.06)290 element = self.wait_for_element_visible(291 selector, by=by, timeout=timeout)292 element.clear()293 pre_action_url = self.driver.current_url294 try:295 if not new_value.endswith('\n'):296 element.send_keys(Keys.CONTROL + "a")297 element.send_keys(new_value)298 else:299 new_value = new_value[:-1]300 element.send_keys(Keys.CONTROL + "a")301 element.send_keys(new_value)302 element.send_keys(Keys.RETURN)303 if Settings.WAIT_FOR_RSC_ON_PAGE_LOADS:304 self.wait_for_ready_state_complete()305 except StaleElementReferenceException:306 self.wait_for_ready_state_complete()307 time.sleep(0.06)308 element = self.wait_for_element_visible(309 selector, by=by, timeout=timeout)310 element.clear()311 if not new_value.endswith('\n'):312 element.send_keys(new_value)313 else:314 new_value = new_value[:-1]315 element.send_keys(new_value)316 element.send_keys(Keys.RETURN)317 if Settings.WAIT_FOR_RSC_ON_PAGE_LOADS:318 self.wait_for_ready_state_complete()319 if (retry and element.get_attribute('value') != new_value and (320 not new_value.endswith('\n'))):321 logging.debug('update_text_value is falling back to jQuery!')322 selector = self.jq_format(selector)323 self.set_value(selector, new_value, by=by)324 def update_text(self, selector, new_value, by=By.CSS_SELECTOR,325 timeout=Settings.SMALL_TIMEOUT, retry=False):326 if self.timeout_multiplier and timeout == Settings.SMALL_TIMEOUT:327 timeout = self._get_new_timeout(timeout)328 self.update_text_value(selector, new_value, by=by,329 timeout=timeout, retry=retry)330 def js_update_date(self,selector, new_value):331 if selector.startswith('#'):332 selector=selector[1:]333 js = "document.getElementById('%s').value='%s'" % (selector, new_value)334 elif "name" in selector:335 selector = selector.split("'")[1]336 js = "document.getElementsByName('%s')[0].value='%s'" % (selector, new_value)337 self.execute_script(js)338 def is_element_present(self, selector, by=By.CSS_SELECTOR):339 if PageUtils.is_xpath_selector(selector):340 by = By.XPATH341 return PageActions.is_element_present(self.driver, selector, by)342 def is_element_visible(self, selector, by=By.CSS_SELECTOR):343 if PageUtils.is_xpath_selector(selector):344 by = By.XPATH345 return PageActions.is_element_visible(self.driver, selector, by)346 def is_element_selected(self, selector, by=By.CSS_SELECTOR):347 if PageUtils.is_xpath_selector(selector):348 by = By.XPATH349 return PageActions.is_element_selected(self.driver, selector, by)350 def is_link_text_visible(self, link_text):351 self.wait_for_ready_state_complete()352 time.sleep(0.01)353 return PageActions.is_element_visible(self.driver, link_text,354 by=By.LINK_TEXT)355 def is_partial_link_text_visible(self, partial_link_text):356 self.wait_for_ready_state_complete()357 time.sleep(0.01)358 return PageActions.is_element_visible(self.driver, partial_link_text,359 by=By.PARTIAL_LINK_TEXT)360 def is_text_visible(self, text, selector, by=By.CSS_SELECTOR):361 self.wait_for_ready_state_complete()362 time.sleep(0.01)363 if PageUtils.is_xpath_selector(selector):364 by = By.XPATH365 return PageActions.is_text_visible(self.driver, text, selector, by)366 def find_visible_elements(self, selector, by=By.CSS_SELECTOR):367 if PageUtils.is_xpath_selector(selector):368 by = By.XPATH369 return PageActions.find_visible_elements(self.driver, selector, by)...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!