10 examples of 'selenium find element by class' in Python

Every line of 'selenium find element by class' 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
this disclaimer
5def find_element(driver, locator):
6 return WebDriverWait(driver, 2).until(
7 EC.presence_of_element_located(locator))
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

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)
99def find_element_by_class_name(self, class_name):
100 """
101 Find an element in the DOM by class name.
102
103 Args:
104 class_name:
105
106 Returns:
107 WebElement
108 """
109 ele = self.soup.find(attrs={'class': class_name})
110 if ele is None:
111 raise NoSuchElementException('The element could not be located by class name: {}'.format(class_name))
112
113 return WebElement(ele, self.current_response, self.current_url, parent=self.id)
54def find_by_text(self, tag, text):
55 return self.wait_until_visible((By.XPATH,"//" + tag + "[contains(.,'" + text + "')]"))
78def _find_element_by_id(self, element_id, driver=None, timeout=5):
79 driver = driver or self._driver
80 return WebDriverWait(driver, timeout, poll_frequency=self._poll_frequency).until(
81 EC.visibility_of_element_located((By.ID, element_id))
82 )
27def click(self, element):
28 element.click()
29 self.body = self.wait_for_body()
30 self.set_shortcuts()
674def assert_visible(self,locator):
675 """
676
677 Verifies that the specified element is both present and visible. An
678 element can be rendered invisible by setting the CSS "visibility"
679 property to "hidden", or the "display" property to "none", either for the
680 element itself or one if its ancestors.
681
682
683 'locator' is an element locator
684 """
685 self.do_command("assertVisible", [locator,])
702def __get_class__(self, class_name, as_list=True, timeout=Global.large_min):
703 wait = WebDriverWait(self.driver, timeout)
704 wait.until(ec.presence_of_element_located((By.CLASS_NAME, class_name)))
705 if as_list:
706 return self.driver.find_elements_by_class_name(class_name)
707 else:
708 return self.driver.find_element_by_class_name(class_name)
249@retry()
250def click(self, element, by=0, check=None):
251 '''Click with retry.
252
253 :param check
254 :type check: int
255 :example check: lambda: self.wait().util(EC.new_window_is_opened())
256 '''
257 self.click_by(element, by=by)
258 # if check is None or not callable(check):
259 # raise TypeError('check must be callable.')
260 if callable(check):
261 check()
165def wait_for_visible(self, element_type, element):
166 """
167 Checks visibility of an element.
168 Keeps checking for visibility until max number of trials self.retry is reached.
169 :param element_type:
170 :param element:
171 :return: :raise:
172 """
173
174 self.check_if_element_present_by_type(element_type, element)
175
176 is_visible = False
177 for i in range(self.retry):
178 print "Wait On Visiblity:: Trial: " + str(i) + " Element Type: " + element_type + ", Element: " + element
179 if element_type is "LINK_TEXT":
180 is_visible = self.driver.find_element_by_link_text(element).is_displayed()
181 elif element_type is "ID":
182 is_visible = self.driver.find_element_by_id(element).is_displayed()
183 elif element_type is "CSS_SELECTOR":
184 is_visible = self.driver.find_element_by_css_selector(element).is_displayed()
185 elif element_type is "XPATH":
186 is_visible = self.driver.find_element_by_xpath(element).is_displayed()
187 elif element_type is "NAME":
188 is_visible = self.driver.find_element_by_name(element).is_displayed()
189
190 if is_visible is True:
191 print "Element " + element + " is visible"
192 break
193 time.sleep(1)
194
195 if is_visible is False:
196 print "Element " + element + " is not visible!"
197
198 return is_visible

Related snippets