selenium错误"element not visible"问题

如题,在使用selenium进行测试的时候,发现会出现”element not visible”的问题。环境如下:

jdk:1.8
selenium:3.3.1
webdriver:chromeDirver

如果元素不可见,那么就没有办法进行操作,如click等。

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

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

参考:http://stackoverflow.com/questions/6101461/how-to-force-selenium-webdriver-to-click-on-element-which-is-not-currently-visib

说一下我的案例:本来是想写一个脚本,打开浏览器,自动输入用户名密码,然后点击确定登录。这里在使用选择器的时候使用了By.className,然后使用driver.findElement(By by)方法,由于findElement方法取出的是一个WebElementlist中的第一个,而正好当时有两个元素,第一个元素的height以及weight都为0导致的。

当然,我遇到的情况比较简单,是人为失误造成的,但是还有可能有其他的情况造成类似的情况。在此总结,欢迎补充

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)来进行操作,具体就不多说的。

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
I'm sorry to hear that you're encountering a `NoSuchElementException` while using Selenium. This exception occurs when the element you're trying to locate or interact with cannot be found on the web page. To troubleshoot this issue, I suggest the following steps: 1. Verify the element locator: Double-check the correctness of the element locator (e.g., ID, class name, XPath, etc.) you are using to locate the element. Ensure that it accurately represents the element you're trying to interact with. 2. Wait for the element to appear: Sometimes, the element may take some time to load on the page. Implement an appropriate wait strategy like `WebDriverWait` to wait for the element to become visible or clickable before interacting with it. 3. Check if the element is present within an iframe: If the element you're trying to locate is within an iframe, you need to switch to that iframe context before locating the element. Use `driver.switch_to.frame()` method to switch to the correct iframe. 4. Ensure the element is not inside a shadow DOM: If the element is inside a shadow DOM, you won't be able to access it directly. You'll need to use `execute_script()` method to execute JavaScript code within the shadow DOM and perform your desired actions. 5. Verify if there are any dynamic elements: Some web pages dynamically load elements or change their structure after initial page load. If this is the case, ensure that you are locating the element after it has been fully loaded or after any dynamic changes have occurred. If none of these steps resolve the issue, providing more specific details about your code and the element you're trying to interact with would be helpful in further troubleshooting.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值