Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

click at at an arbitrary position in web browser with selenium 2 (Python binding)

I am getting confused trying to find a way to send the left-click in a web browser window in a specific point. I am using Selenium selenium-2.44.0 and Python 2.7. My ultimate goal is to be able to click in certain areas in the window, but for now I just want to make sure I am able to click at all. I thought that clicking on an area where a link is located is a good idea because then I will be able to verify the click did occur (i.e., I will be taken to another html page).

All the prerequisites are met, I am able to start the browser and access and manipulate various web elements. From what I've found in the Help, one is supposed to use move_by_offset method to move the mouse cursor into a certain direction. However, when I run the code, the click doesn't occur (or it occurs, but the url link is not clicked and no new page opens). I cannot even verify whether the click occurred. When clicking the Logout link, for instance, in a normal browser session, the logout operation occurs.

...
homeLink = driver.find_element_by_link_text("Home")
homeLink.click() #clicking on the Home button and mouse cursor should? stay here
print homeLink.size, homeLink.location

helpLink = driver.find_element_by_link_text("Help")
print helpLink.size, helpLink.location

action = webdriver.common.action_chains.ActionChains(driver)
action.move_by_offset(150,0) #move 150 pixels to the right to access Help link
action.click()
action.perform()

Here is the screenshot of the area in the web page I am working with.

enter image description here

The size and location of the elements are printed as below:

{'width': 39, 'height': 16} {'y': 47, 'x': 341}
{'width': 30, 'height': 16} {'y': 47, 'x': 457}

The html code behind the page is below, if it's worth looking at.

<a href="/web/">Home</a>
|
<a href="/web/Account/LogOff">Logout</a>
|
<a href="#" onclick="HelpFile.open();">Help</a>

I know that I can access the link by finding an element in many ways, but I am trying to perform a click in a certain location and am using the link element just to verify the click really occurred.

How do perform the click?

like image 715
Alex Tereshenkov Avatar asked Jan 14 '15 17:01

Alex Tereshenkov


1 Answers

Hard to say for you exact situation but I know a workaround to the question in the summary and bold

send the left-click in a web browser window in a specific point

You just use execute_script and do the clicking using javascript.

self.driver.execute_script('el = document.elementFromPoint(47, 457); el.click();')

It's convenient in a pinch because you can debug find the coordinates of an element in a browser by opening console and using querySelector (works the same as Webdriver's By.CSS_SELECTOR):

el = document.querySelector('div > h1');
var boundaries = el.getBoundingClientRect();
console.log(boundaries.top, boundaries.right, boundaries.bottom, boundaries.left);

That being said, it's a really bad idea to write your tests to click a specific point. But I have found that sometimes even when el.click() or an action chain aren't working, execute script will still work using a querySelector which is about as good as what you would be doing in Selenium.

self.driver.execute_script('el = document.querySelector("div > h1"); el.click();')
like image 128
Cynic Avatar answered Oct 05 '22 23:10

Cynic