10 examples of 'python selenium wait for element' in Python

Every line of 'python selenium wait for element' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
52def wait_for(self, interval=None, timeout=None):
53 """
54 Get a ElementWaitFor instance.
55
56 :param interval: the wait interval (in milliseconds). If None, use element's wait interval.
57 :param timeout: the wait timeout (in milliseconds). If None, use element's wait timeout.
58 """
59 _interval = self.get_wait_interval() if interval is None else interval
60 _timeout = self.get_wait_timeout() if timeout is None else timeout
61 return ElementWaitFor(self, _interval, _timeout)
240def wait_for_element(self, identifier, by=By.ID, timeout=5, visible=False):
241 visibility = EC.visibility_of_element_located
242 presence = EC.presence_of_element_located
243 loader = visibility if visible else presence
244 load = loader((by, identifier))
245 WebDriverWait(self.drv, timeout).until(load)
1716def wait_for_condition(self,script,timeout):
1717 """
1718 Runs the specified JavaScript snippet repeatedly until it evaluates to "true".
1719 The snippet may have multiple lines, but only the result of the last line
1720 will be considered.
1721
1722
1723 Note that, by default, the snippet will be run in the runner's test window, not in the window
1724 of your application. To get the window of your application, you can use
1725 the JavaScript snippet ``selenium.browserbot.getCurrentWindow()``, and then
1726 run your JavaScript in there
1727
1728
1729 'script' is the JavaScript snippet to run
1730 'timeout' is a timeout in milliseconds, after which this command will return with an error
1731 """
1732 self.do_command("waitForCondition", [script,timeout,])
170def wait_for_element(browser, locator, timeout=12, poll_frequency=0.5):
171 '''
172 '''
173 try:
174 element = WebDriverWait(
175 browser, timeout, poll_frequency).until(
176 expected_conditions.presence_of_element_located(locator),
177 message='Element {} could not be found.'.format(locator[1]))
178 wait_for_ajax(browser, poll_frequency=poll_frequency)
179 return element
180 except TimeoutException as err:
181 print(
182 '{}: Waiting for element "{}" to display. {}'.format(
183 type(err).__name__,
184 locator[1],
185 err
186 ))
187 return None
227def waitForElements(self, css_selector, timeout=DEFAULT_WAIT_TIMEOUT,
228 visible=None):
229 """ Waits for DOM elements matching the provided CSS selector to be
230 available.
231
232 Args:
233 - css_selector: CSS selector string
234
235 Kwargs:
236 - timeout: Operation timeout in seconds
237 - visible: Ensure elements visibility status:
238 * True: wait for visible elements only
239 * False: wait for invisible elements only
240 * None: skip visibility status checks
241
242 Returns: List of DOM elements
243 """
244 def get_element_checker(driver, visible):
245 locator = (By.CSS_SELECTOR, css_selector)
246 if visible is True:
247 return EC.visibility_of_element_located(locator)
248 if visible is False:
249 return EC.invisibility_of_element_located(locator)
250 return lambda _: driver.find_elements_by_css_selector(css_selector)
251
252 message = u"Couldn't find elems matching %s (visibility check: %s)" % (
253 css_selector, visible)
254 WebDriverWait(self, timeout, poll_frequency=.25).until(
255 get_element_checker(self, visible), message=message)
256
257 return self.find_elements_by_css_selector(css_selector)
80def wait_on_element(driver, xpath, timeout=120):
81 num = 0
82 while is_element_present(driver, xpath) is False:
83 if num >= timeout:
84 return False
85 else:
86 num += 1
87 time.sleep(1)
88 return True
79def wait_for_element(self, using=Locator.ID.value, value=None):
80 return (using, value)
54def wait_for_element_not_visible(self, *locator):
55 count = 0
56 while self.is_element_visible(*locator):
57 time.sleep(1)
58 count += 1
59 if count == self.timeout:
60 raise Exception(':'.join(locator) + " is still visible")
314def wait_for_exact_text_visible(driver, text, selector, by=By.CSS_SELECTOR,
315 timeout=settings.LARGE_TIMEOUT):
316 """
317 Searches for the specified element by the given selector. Returns the
318 element object if the text matches exactly with the text in the element,
319 and the text is visible.
320 Raises an exception if the text or element do not appear
321 in the specified timeout.
322 @Params
323 driver - the webdriver object (required)
324 text - the exact text that is expected for the element (required)
325 selector - the locator for identifying the page element (required)
326 by - the type of selector being used (Default: By.CSS_SELECTOR)
327 timeout - the time to wait for elements in seconds
328 @Returns
329 A web element object that contains the text searched for
330 """
331 element = None
332 start_ms = time.time() * 1000.0
333 stop_ms = start_ms + (timeout * 1000.0)
334 for x in range(int(timeout * 10)):
335 try:
336 element = driver.find_element(by=by, value=selector)
337 if element.is_displayed() and text.strip() == element.text.strip():
338 return element
339 else:
340 element = None
341 raise Exception()
342 except Exception:
343 now_ms = time.time() * 1000.0
344 if now_ms >= stop_ms:
345 break
346 time.sleep(0.1)
347 plural = "s"
348 if timeout == 1:
349 plural = ""
350 if not element:
351 raise ElementNotVisibleException(
352 "Expected exact text {%s} for {%s} was not visible "
353 "after %s second%s!" % (text, selector, timeout, plural))
63def wait_until_element_not_visible(self, locator, timeout=10):
64 '''
65 Search element by locator and wait until it is not visible
66 '''
67 # Remove implicit wait
68 self.driver.implicitly_wait(0)
69 # Wait for invisibility
70 WebDriverWait(self.driver, timeout).until(EC.invisibility_of_element_located(locator))
71 # Restore implicit wait from properties
72 self.set_implicit_wait()

Related snippets