Selenium-异常:Element is not currently visible and so may not be interacted with的解决
2017-12-21 14:18
507 查看
Selenium-异常:Element is not currently visible and so may not be interacted with的解决
解决方法:
思路: 使用findElements遍历,input标签,然后找到share按钮对应的 索引值,然后再操作。
List<WebElement>element=driver.findElements(By.tagName("input"));
for(WebElement e:element)
{
System.out.println(e.getAttribute("id"));
}
element.get(78).click();
至此 问题解决。
问题更正: 此种问题的关键是在于用className和id都不唯一所以找不到对象,改为:
driver.findElement(By.xpath("//input[@value='Share']")).click();同样可以解决问题。
二、
我们需要本地上传文件时,一般找个那个input元素,然后send_keys把本地的文件路径传进去就好了。像这样:
driver.find_element_by_name("files").send_keys("D:\\manual.pdf")
但是最近遇到一个问题,上传的时候报错啦。报错信息如下:
selenium.common.exceptions.ElementNotVisibleException: Message: u'Element is not currently visible and so may not be interacted with' ; Stacktrace:
它这说的visible和css里元素的visibility属性好像还不是一个东西,就算用js改变这个元素的CSS属性为可见的还是会报上面这个错误。后来看到webElement类里有个方法叫is_displayed()解释是"Whether the element would be visible to a user" 所以这里说的可不可见是对用户来说的,之所以会报不可见的错是因为本地上传里的input是经过美化的,已经看不到了,所以需要用js改变这个元素的样式把本来面目显示出来~~~ 这就需要自己调样式了。Js里有个attr方式可以用来改变元素的样式,示例如下:
driver.execute_script("$('.fileinput-button input').eq(0).attr('style','height:20px;opacity:1;display:block;position:static;transform:translate(0px, 0px) scale(1)')")
改了样式后,找到该元素调用了一下is_displayed(),返回值是true了,再运行脚本就不会报错,文件顺利上传上去了~~
原来的上传按钮:
被还原出来的input 如下:
总结:遇到被美化的元素,导致出现元素不可见的异常时,就用JS把它的样式还原回去~
解决方法:
思路: 使用findElements遍历,input标签,然后找到share按钮对应的 索引值,然后再操作。
List<WebElement>element=driver.findElements(By.tagName("input"));
for(WebElement e:element)
{
System.out.println(e.getAttribute("id"));
}
element.get(78).click();
至此 问题解决。
问题更正: 此种问题的关键是在于用className和id都不唯一所以找不到对象,改为:
driver.findElement(By.xpath("//input[@value='Share']")).click();同样可以解决问题。
二、
我们需要本地上传文件时,一般找个那个input元素,然后send_keys把本地的文件路径传进去就好了。像这样:
driver.find_element_by_name("files").send_keys("D:\\manual.pdf")
但是最近遇到一个问题,上传的时候报错啦。报错信息如下:
selenium.common.exceptions.ElementNotVisibleException: Message: u'Element is not currently visible and so may not be interacted with' ; Stacktrace:
它这说的visible和css里元素的visibility属性好像还不是一个东西,就算用js改变这个元素的CSS属性为可见的还是会报上面这个错误。后来看到webElement类里有个方法叫is_displayed()解释是"Whether the element would be visible to a user" 所以这里说的可不可见是对用户来说的,之所以会报不可见的错是因为本地上传里的input是经过美化的,已经看不到了,所以需要用js改变这个元素的样式把本来面目显示出来~~~ 这就需要自己调样式了。Js里有个attr方式可以用来改变元素的样式,示例如下:
driver.execute_script("$('.fileinput-button input').eq(0).attr('style','height:20px;opacity:1;display:block;position:static;transform:translate(0px, 0px) scale(1)')")
改了样式后,找到该元素调用了一下is_displayed(),返回值是true了,再运行脚本就不会报错,文件顺利上传上去了~~
原来的上传按钮:
被还原出来的input 如下:
总结:遇到被美化的元素,导致出现元素不可见的异常时,就用JS把它的样式还原回去~
相关文章推荐
- Selenium-异常:Element is not currently visible and so may not be interacted with的解决
- Webdriver Element is not currently visible and so may not be interactedwith 解决方法
- Element is not currently visible and so may not be interactedwith 解决方法
- selenium webDriver: Element is not currently visible and so may not be interacted with
- selenium webdriver使用过程中出现Element is not currently visible and so may not be interacted with的处理方法
- WebDriver遇到Element is not currently visible and so ...解决
- selenium +Python 测试时报 “Element is not visible”解决方法
- Webdriver Element is not currently visible 解决方法
- The function getUserId must be used with a prefix when a default namespace is not specified 解决办法
- SharePoint 2013 搜索报错"Unable to retrieve topology component health. This may be because the admin component is not up and running"
- 解决Selenium报错“Element is not clickable at point...”的四种方法
- SharePoint 2013 搜索报错"Unable to retrieve topology component health. This may be because the admin component is not up and running"
- Hadoop2.7启动报Error: JAVA_HOME is not set and could not be found解决办法
- the type List is not generic; it cannot be parameterized with arguments<T>解决办法
- Scrapped or attached views may not be recycled. isScrap:false isAttached:true异常
- Error:JAVA_HOME is not set and could not be found 解决般的法
- 【解决办法】Hadoop安装完后,启动时报Error: JAVA_HOME is not set and could not be found.
- A class file was not written. The project may be inconsistent, if so try refreshing this project and building it. eclipse提示错误
- 解决iText 5.0.1生成pdf,加入iTextAsian.jar 出现异常 Font 'STSong-Light' with 'UniGB-UCS2-H' is not recognized.的
- <转> 解决异常:IllegalStateException: Fragment <ThisFragment> is not currently in the FragmentManager