单击元素的底部?

时间:2017-06-05 18:26:53

标签: java selenium

我有一个我需要点击的链接,它位于另一个元素的下方。像这样:

______________________
| A  ____________    | <-- on top
|____|B_________|____|
     |__________| < - on bottom (need to click this one)

当我尝试点击底部链接时,我得到了

(B) is not clickable at point (254, 5). Other element would receive the click: (A).  

Here是一张图片(我需要点击PO Box链接)。我不知道到底该做什么。谢谢!

这是有问题的HTML。 Selenium正在将点击操作发送到范围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 -->

4 个答案:

答案 0 :(得分:2)

如果是这种情况你可以做一些解决方法,比如点击元素的底部

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();

答案 1 :(得分:0)

我认为你可以使用 xpath = //p[@class="helpLink"]/a

答案 2 :(得分:0)

这主要发生在Chrome中。 Chrome不会计算元素的确切位置,并且始终会在元素中间单击。

要解决此问题,请获取元素坐标,然后单击链接

 var canvas = document.getElementById("mycanvas");

 var ctx = canvas.getContext("2d");


var ball = function() {

this.x = Math.floor((Math.random() * 10) + 1);
this.y = Math.floor((Math.random() * 10) + 1);
this.xSpeed = Math.floor((Math.random() * 10) + 1);
this.ySpeed = Math.floor((Math.random() * 10) + 1);

};


  var balls =[];

for(i = 0 ; i < 10 ; i++) {

balls[i] = new ball();

}



  function draw() {
 for( i = 0; i < 10 ; i++) {

var ball = balls[i]

ctx.beginPath()

if(ball.x < 0 || ball.x > 400) {

ball.xSpeed = -ball.xSpeed;
}

if(ball.y < 0 || ball.y > 400 ) {

ball.ySpeed = -ball.ySpeed;
}

ball.x += ball.xSpeed;

ball.y += ball.ySpeed;

ctx.arc(ball.x , ball.y , 2 , 0 , Math.PI * 2 , false);
ctx.stroke();

ctx.fill();
}
}

  function bounce() {

setInterval(draw , 10);

}

答案 3 :(得分:0)

我建议您以更广泛的方式查看问题并实现一种方法来单击元素上的任何位置。以下是代码段。我在C#中编写了以下内容并成功进行了测试。 x和y坐标是相对于元素大小分配的,这使得更容易对元素执行操作。

您可以实现ClickElementAt方法并调用如下方法。

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();
    }