有时候我们会碰到一些元素不可见,这个时候selenium就无法对这些元素进行操作了。例如,下面的情况:

页面主要通过“display:none”来控制整个下拉框不可见。这个时候如果直接操作这个下拉框,就会提示:

from selenium import webdriver
from selenium.webdriver.support.select import Select
import os,time driver = webdriver.Chrome()
file_path = 'file:///' + os.path.abspath('test.html')
driver.get(file_path) sel = driver.find_element_by_tag_name('select')
Select(sel).select_by_value('opel')
time.sleep(2) driver.quit()

exceptions.ElementNotVisibleException: Message: element not visible: Element is not currently visible and may not be manipulated

我们需要通过javaScript修改display的值。

js = 'document.querySelectorAll("select")[0].style.display="block";'
driver.execute_script(js) sel = driver.find_element_by_tag_name('select')
Select(sel).select_by_value('opel')

document.querySelectorAll("select")[0].style.display="block";

document.querySelectorAll("select")  选择所有的select。

[0] 指定这一组标签里的第几个。

style.display="block";  修改样式的display="block" ,表示可见。

执行完这句js代码后,就可以正常操作下拉框了。

Java代码

import java.io.File;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.By.ById;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.JavascriptExecutor; public class SelectTest { public static void main(String[] args){ WebDriver driver = new ChromeDriver();
File file = new File("C:/Users/fnngj/Desktop/test.html");
String filePath = file.getAbsolutePath();
driver.get(filePath); String js = "document.querySelectorAll('select')[0].style.display='block';";
((JavascriptExecutor)driver).executeScript(js); Select sel = new Select(driver.findElement(ById.xpath("//select")));
sel.selectByValue("opel"); }
}

Selenium Webdriver——操作隐藏的元素display属性的更多相关文章

  1. Selenium Webdriver——操作隐藏的元素(二)display属性

    有时候我们会碰到一些元素不可见,这个时候selenium就无法对这些元素进行操作了.例如,下面的情况: 页面主要通过“display:none”来控制整个下拉框不可见.这个时候如果直接操作这个下拉框, ...

  2. Selenium Webdriver——操作隐藏的元素(三)switchTo().frame()

    在web 应用中经常会遇到frame 嵌套页面的应用,页WebDriver 每次只能在一个页面上识别元素,对于frame 嵌套内的页面上的元素,直接定位是定位是定位不到的.这个时候就需要通过switc ...

  3. Selenium Webdriver——操作隐藏的元素

    有时候我们会碰到一些元素不可见,这个时候selenium就无法对这些元素进行操作了.例如,下面的情况: 页面主要通过“display:none”来控制整个下拉框不可见.这个时候如果直接操作这个下拉框, ...

  4. Selenium Webdriver——操作隐藏的元素(四)

    页面上弹出的对话框是自动化测试经常会遇到的一个问题:很多情况下对话框是一个iframe,如上一节中介绍的例子,处理起来稍微有点麻烦:但现在很多前端框架的对话框是div 形式的,这就让我们的处理变得十分 ...

  5. selenium在操作隐藏元素时会报错,怎么判断元素是隐藏的?

    首先页面元素隐藏有五种方法: 1. opacity: 0; opacity 属性的意思是设置一个元素的透明度.它不是为改变元素的边界框(bounding box)而设计的.这意味着将 opacity ...

  6. js操作:selenium无法操作隐藏元素问题

    对于前端隐藏元素,一直是selenium自动化定位元素的隐形杀手,脚本跑到隐藏元素时位置时报各种各样的错误, 隐藏的下拉菜单又没有办法避免,此帖只为交流隐藏元素自动化定位处理方法(3种操作)      ...

  7. Python+selenium(操作隐藏元素)

    测试过程中,偶尔会碰到一些页面的隐藏元素,如下,是小编举的一个简单的例子: test.html <html> <head></head> <body> ...

  8. selenium操作隐藏的元素

    有时候我们会碰到一些元素不可见,这个时候selenium就无法对这些元素进行操作了.例如,下面的情况: Python 页面主要通过“display:none”来控制整个下拉框不可见.这个时候如果直接操 ...

  9. selenium操作隐藏的元素 (下拉框类型)

    有时候我们会碰到一些元素不可见,这个时候selenium就无法对这些元素进行操作了.例如,下面的情况: Python 页面主要通过“display:none”来控制整个下拉框不可见.这个时候如果直接操 ...

随机推荐

  1. Atitit. C# java 的api 文件夹封装结构映射总结

    Atitit. C#  java 的api 文件夹封装结构映射总结 C# java ref System.Reflection System.Type. java.lang.ref concurren ...

  2. Ambarella SDK build 步骤解析

    Make Target Options make命令如下: make <Tab> <Tab> /*列出所有支持的目标(命令行输入make, 再按两下Tab键)*/ make & ...

  3. oc34--instancetype和id的区别

    // Person.h #import <Foundation/Foundation.h> @interface Person : NSObject @property int age; ...

  4. C# 你什么让程序员寂寞成酱紫 (男生版 娱乐中学习 抽象类 接口 继承 实现方法 )

    你什么让程序员寂寞成酱紫 (男生版 娱乐中学习 抽象类 接口 继承 实现方法 )   一个家庭 相当于 一个空间,这个空间里 有 很多元素,比如 爱,爱这个抽象事物,可能有很多动作,接吻.交流,对于一 ...

  5. C#如何读写和创建INI文件(经典)转

    C#如何读写和创建INI文件 分类: c#程序设计2011-11-27 20:42 4935人阅读 评论(2) 收藏 举报 inic#stringbuffernullfile 在做项目过程中,有时需要 ...

  6. Coursera Algorithms week2 栈和队列 练习测验: Queue with two stacks

    题目原文: Implement a queue with two stacks so that each queue operations takes a constant amortized num ...

  7. php文件,文件夹

    例子代码:<?php$f='/www/htdocs/index.html';$path_parts = pathinfo($f);echo $path_parts['dirname'], &qu ...

  8. poj3233Matrix Power Series(矩阵乘法)

    Matrix Power Series Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 23187   Accepted: ...

  9. iOS 点击事件传递及响应

    1.iOS中的事件 iOS中的事件可以分为3大类型: 触摸事件 加速计事件 远程控制事件这里我们只讨论iOS中的触摸事件. 1.1响应者对象(UIResponder) 在iOS中不是任何对象都能处理事 ...

  10. Android Studio and Gradle安装心得

    安装基于Eclipse 的ADT一段时间,感觉确实有很多功能不足,通过网上资料,决定改向AS. AS下载了最新的2.3版本,它不分64位与32位,网上说有单独版是瞎扯蛋.只要启动不同的EXE就行了. ...