Search code examples
pythonseleniumselenium-webdriverwebdriverclassname

Selenium Finding hover button element by class name in python


Happy Sunday,

Trying to click a filter button (called offer) on a website. It is a hover button that does not have an associated link so cannot find element by link text.

When clicking Offer button, the wanted button goes from class="item-list-header-filter-icon item-list-wanted-filter hover-state" to class="item-list-header-filter-icon item-list-wanted-filter hover-state inactive"

I have tried:

driver.find_element_by_class_name(item-list-header-filter-icon item-list-wanted-filter hover-state)

and since it is a dynamic, I made webdriver wait:

        try:
        element = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.CLASS_NAME, "item-list-header-filter-icon item-list-wanted-filter hover-state inactive"))
        )
        element.click()

Any ideas much appreciated! Have a good day


Solution

  • You need to perform a hover over that element.
    It can be done as following:

    from selenium.webdriver.common.action_chains import ActionChains
    
    offer = driver.find_element_by_css_selector('.item-list-header-filter-icon.item-list-offer-filter')
    hover = ActionChains(driver).move_to_element(offer)
    hover.perform()
    

    In case all you asked for is to locate that element you can simply do it with

    offer = driver.find_element_by_css_selector('.item-list-header-filter-icon.item-list-offer-filter')