Webdriver 解决 “Element not visible”

今天下午进行webdriver自动化测试,测试脚本运行报错 element not visible 如下图:

 

在此记录一下如何解决此问题,为以后填坑。。。

问题分析

首先,分析一下什么是“element not visible”。 
在selenium中,如果元素满足以下的条件,那么才是“可见的”:

  •  
  • visibility != hidden
  • display != none(其父元素包含此属性也是一样的)
  • opacity != 0 (点击时并不会如此校验)
  • height以及width 都 > 0
  • 对于input标签,没有hidden属性

     

我遇到的应该是页面元素没有渲染完全,在操作前进行适当的等待即可!

但是进行click(),无响应:

解决办法:

1.The element is not visible to click.

Use Actions or JavascriptExecutor for making it to click.

By Actions:

WebElement element = driver.findElement(By("element_path"));
Actions actions = new Actions(driver);
actions.moveToElement(element).click().perform();

By JavascriptExecutor:

JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("scroll(250, 0)"); // if the element is on top.
jse.executeScript("scroll(0, 250)"); // if the element is on bottom.
or
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("arguments[0].scrollIntoView()", Webelement); 

Then click on the element.

2.The page is getting refreshed before it is clicking the element.

For this, make the page to wait for few seconds.

3. The element is clickable but there is a spinner/overlay on top of it

The below code will wait until the overlay disppears

By loadingImage = By.id("loading image ID");
WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
wait.until(ExpectedConditions.invisibilityOfElementLocated(loadingImage));

Then click on the element.

参考链接:https://www.cnblogs.com/woniu123/p/6038063.html

下面列举出常见的情况:

1、页面元素没有渲染完全

这种情况,比如点击后,需要渲染页面元素,然后插入dom等操作,很有可能暂时还没有你想要的dom,那么就需要进行等待。方法有两种:

第一种:

try{
    Thread.sleep(3000);
}catch(Exception e){
    //TODO
}

第二种:

By by = By.id('id');
//等待3s直到这个元素可以点击,否则抛出异常
new WebDriverWait(webDriver,3).until(ExpectedConditions.elementToBeClickable(by)));
这个方法,就是等待。这个ExpectedConditions条件有很多可以供你选择,比如可视等等,大家可以自己看看。

2、版本问题

我们直到selenium这个东西,历经3代,对于webdriver的支持也不太一样。在selenium3中,firefox的dirver就不是内置的了,需要手动下载,且对firefox浏览器版本也有要求,同时其他的浏览器也都有对应的驱动。总体而言在selenium2中,由于除了firefox外所有的驱动都不是官方开发的,所以说有可能出现不兼容的情况。

3、就是元素选择错了

这里要注意的就是,多用id、xpath来选择,少用className来选择。

4、需要有前置条件的

这个情况比较特殊,比如必须要在某个元素hover的情况下,才能够有一个弹框,然后才能进行一些操作,那么这个时候,就需要进行一些操作。主要就是使用Actions类进行标记:

WebDriver webDirver = new ChromeDriver();
Actions actions = new Actions(webDriver);
By by = By.id('id');
actions.moveToElement(webDriver.findElement(by)).build().perform();

//弹框出来后继续的继续的操作

5、页面美化导致的情况

我们正常情况化会对元素进行一些美化,比如,文件上传的框,一般的做法就是将原有的input元素的透明度opacity设置为0,然后放在一个自己定义的好看的元素上,这样就能够点击却不能够进行其他的操作。这里如果想要对其进行其他操作,需要先把他的样式恢复过来。这里可以使用js进行恢复。使用executeScript(String s, Object... args)来进行操作,具体就不多说的。

参考链接:https://blog.csdn.net/BerlinLove/article/details/70053808

参考链接:https://www.cnblogs.com/woniu123/p/6038063.html

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值