MCPcopy Create free account
hub / github.com/seleniumbase/SeleniumBase / hover_and_click

Function hover_and_click

seleniumbase/fixtures/page_actions.py:273–321  ·  view source on GitHub ↗

Fires the hover event for a specified element by a given selector, then clicks on another element specified. Useful for dropdown hover based menus. @Params driver - the webdriver object (required) hover_selector - the css selector to hover over (required) click_selector - th

(
    driver,
    hover_selector,
    click_selector,
    hover_by="css selector",
    click_by="css selector",
    timeout=settings.SMALL_TIMEOUT,
    js_click=False,
)

Source from the content-addressed store, hash-verified

271
272
273def hover_and_click(
274 driver,
275 hover_selector,
276 click_selector,
277 hover_by="css selector",
278 click_by="css selector",
279 timeout=settings.SMALL_TIMEOUT,
280 js_click=False,
281):
282 """
283 Fires the hover event for a specified element by a given selector, then
284 clicks on another element specified. Useful for dropdown hover based menus.
285 @Params
286 driver - the webdriver object (required)
287 hover_selector - the css selector to hover over (required)
288 click_selector - the css selector to click on (required)
289 hover_by - the hover selector type to search by (Default: "css selector")
290 click_by - the click selector type to search by (Default: "css selector")
291 timeout - number of seconds to wait for click element to appear after hover
292 js_click - the option to use js_click() instead of click() on the last part
293 """
294 _reconnect_if_disconnected(driver)
295 start_ms = time.time() * 1000.0
296 stop_ms = start_ms + (timeout * 1000.0)
297 element = driver.find_element(by=hover_by, value=hover_selector)
298 hover = ActionChains(driver).move_to_element(element)
299 for x in range(int(timeout * 10)):
300 try:
301 hover.perform()
302 element = driver.find_element(by=click_by, value=click_selector)
303 if js_click:
304 driver.execute_script("arguments[0].click();", element)
305 else:
306 element.click()
307 return element
308 except Exception:
309 now_ms = time.time() * 1000.0
310 if now_ms >= stop_ms:
311 break
312 time.sleep(0.1)
313 plural = "s"
314 if timeout == 1:
315 plural = ""
316 message = "Element {%s} was not present after %s second%s!" % (
317 click_selector,
318 timeout,
319 plural,
320 )
321 timeout_exception(NoSuchElementException, message)
322
323
324def hover_element_and_click(

Callers

nothing calls this directly

Calls 6

timeout_exceptionFunction · 0.85
find_elementMethod · 0.45
execute_scriptMethod · 0.45
clickMethod · 0.45
sleepMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…