Igor Simoes
Igor Simoes

Reputation: 21

Image Map Clicking in Selenium

I have to test an application that has a huge image, but just a small portion of it is clickable through image maps.

I have tried everything to calculate the right position and click, but the click is outside the position and the test fails.

Could someone point me on how to log current mouse position or how to show the mouse during the test?

I am using action chains to move the mouse cursor to the center of the image, but from there all my calculations made the click outside the image map rectangle.

See below my code snippet:

        el=driver.find_elements_by_xpath("/html/body/form/table/tbody/tr/td/img")[0]
        #el=driver.find_elements_by_xpath("//html/body/map/area[2]")[0]
        width=el.size["width"]
        height=el.size["height"]        

        action = webdriver.common.action_chains.ActionChains(driver)
        action.move_to_element(el)
        print driver.get_window_position()
        action.move_by_offset(193, 310)
        print driver.get_window_position()
        action.click()
        action.perform()

Thanks,

Upvotes: 1

Views: 1180

Answers (2)

Igor Simoes
Igor Simoes

Reputation: 21

Just as a reference. I ended up finding a solution.

# Wait for the map to show up and navigate to account page
try:
    WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "Map")))
except Exception, e:
    print "Image with token toot long to load..."
    raise AssertionError()

href = driver.find_element_by_xpath("//html/body/map/area[2]").get_attribute("href")
driver.get(href)

I could see that the click would just fire the get to the HREF, so instead of heaving to find exactly where to click, I put the href on a var, and then I do a get on that href.

Script is working nicely and even better it does not depend on screen resolution.

Upvotes: 1

Sers
Sers

Reputation: 12255

Did you try click using javascript?

driver.execute_script("arguments[0].click();", element)

Upvotes: 0

Related Questions