Reputation: 3534
I have a link that I need to click on that is halfway underneath another element. Something like this:
______________________
| A ____________ | <-- on top
|____|B_________|____|
|__________| < - on bottom (need to click this one)
When I try to click on the bottom link, I get
(B) is not clickable at point (254, 5). Other element would receive the click: (A).
Here is an image (I need to click on the PO Box link). I don't know exactly what to do here. Thanks!
Here is the HTML in question. Selenium is sending the click action to the span labelFieldWrapper
:
<div class='clr type_address formFieldContainer'>
<span class="labelFieldWrapper">
<label class="placeholder" for="address1">
<span class="labelText">Address 1</span>
</label>
<input id="address1" name="address1" type="text" value="" autofillparam="ON" size="20" maxlength="60"/><span class="asterisk">*</span>
</span>
<p class="helpLink">
<a href="javascript: void(0)"
onclick="window.open('/checkout/canadian_province_support.jsp',
'pobox',
'width=450, \
height=200, \
directories=no, \
location=no, \
menubar=no, \
resizable=no, \
scrollbars=1, \
status=no, \
toolbar=no');
return false;">PO Box?</a> <-- Trying to click on this link
</p>
</div><!-- /formFieldContainer -->
Upvotes: 0
Views: 2660
Reputation: 120
I would suggest you to look at the problem in a broader way and implement a method to click anywhere on the element. The below is the code snippet. I wrote the below in C# and successfully able to test. The x and y co-ordinates are assigned with respect to element size which makes it easier to perform the action on the element.
You can implement a method ClickElementAt and call the method like below.
ClickElementAt(element,Bottom);//Calling the method here
//Implementation
public void ClickElementAt(Element element, ClickPoint clickPoint)
{
int x, y;
switch (clickPoint)
{
case ClickPoint.Center:
x = element.Size.Width / 2;
y = element.Size.Height / 2;
break;
case ClickPoint.Top:
x = element.Size.Width / 2;
y = element.Size.Height / 10;
break;
case ClickPoint.Left:
x = element.Size.Width / 10;
y = element.Size.Height / 2;
break;
case ClickPoint.Right:
x = element.Size.Width * 90 / 100;
y = element.Size.Height / 2;
break;
case ClickPoint.Bottom:
x = element.Size.Width / 2;
y = element.Size.Height * 90 / 100;
break;
default:
throw new ArgumentOutOfRangeException("clickPoint");
}
var actions = new Actions(this.driver);
actions
.MoveToElement(element, x, y)
.Click()
.Perform();
}
Upvotes: 0
Reputation: 2583
if this case you can make some workaround, like click on the bottom of element
WebElement element = driver.findElement(By.xpath("someXpath"));
int halfOfHeight = element.getSize().getHeight()/2;
// moveToElement* method moves to the middle of element, so we'll also move on half of element and click on the 3rd pix from the bottom
int offset = halfOfHeight - 3;
Actions actions = new Actions(driver);
actions
.moveToElement(element)
.moveByOffset(0, offset)
.click()
.build()
.perform();
Upvotes: 3
Reputation: 72
This mostly happens in Chrome. Chrome does not calculate the exact location of element and always click in the middle of Element.
To fix this, get the element coordinate and then click on the link
WebElement link= driver.findElement(By.xpath("xpath of link")); ((JavascriptExecutor)driver).executeScript("window.scrollTo(0,"+link.getLocation().x+")"); link.click();
Upvotes: 1