python+selenium运行时,提示元素不可见
python+selenium运行多次新增项目脚本(出错的元素通过by_id的方式定位),当第三次新增时报Message: element not visible的错误,加入等待时间,等页面加载完成,仍旧报相同的错误,错误如下图:

分析原因:前两次新增都是成功的,说明定位元素的代码没有问题; 加入等待时间还是报错,排除了页面没有加载完成的原因
打开开发者模式(F12),在控制台用id进行定位,发现出现了两个元素:
- 0:input#bankAccountType1.form-control
- 1:input#bankAccountType1
其中索引为1的元素是正常的,鼠标放到上面,浏览器也可识别出来,但索引为0的元素有后缀.form-control,鼠标放到上面浏览器也不能识别,具体怎么出来的这个元素,目前还不太清楚需要进一步探究。
进一步试验发现第二次新增时,通过id进行定位发现该元素就出现了两次。也就是说通过id定位出两个相同元素,但是脚本中对元素的操作是单元素操作,所以程序无法识别出元素,就报上述错误。

解决方法:
方法一:
先定位出元素列表,判断元素的大小,更改索引值
def choose_account_type(self):
choice_account_type = random.randint(1, 2)
if choice_account_type == 1:
css_account_type_current_account = self.add_merchant_v2_config_ini.get_ini_value \
("add_merchant_v2.ini", "bank_accounts", "current_account")
el_account_type_current_account_list = self.driver.getElenments(css_account_type_current_account)
if len(el_account_type_current_account_list) > 1:
n = 1
else:
n = 0
self.driver.click1(el_account_type_current_account_list[n])
else:
css_account_type_current_account = self.add_merchant_v2_config_ini.get_ini_value \
("add_merchant_v2.ini", "bank_accounts", "saving_account")
el_account_type_current_account_list = self.driver.getElenments(css_account_type_current_account)
if len(el_account_type_current_account_list) > 1:
n = 1
else:
n = 0
self.driver.click1(el_account_type_current_account_list[n])

方法二:
在函数中增加一个参数,当n超过1时,先定位出一组元组,然后再对其中的元素进行操作。代码如下:
def choose_account_type(self, n=0):
if n < 1:
choice_account_type = random.randint(1, 2)
if choice_account_type == 1:
css_account_type_current_account = self.add_merchantR2_config_ini.get_ini_value("add_merchantR2.ini",
"bankaccounts",
"current_account")
self.driver.click(css_account_type_current_account)
else:
css_account_type_saving_account = self.add_merchantR2_config_ini.get_ini_value("add_merchantR2.ini",
"bankaccounts",
"saving_account")
self.driver.click(css_account_type_saving_account)
else:
time.sleep(0.5)
choice_account_type = random.randint(1, 2)
if choice_account_type == 1:
css_account_type_current_account = self.add_merchantR2_config_ini.get_ini_value("add_merchantR2.ini",
"bankaccounts",
"current_account")
el_account_type_current_account_list = self.driver.getElenments(css_account_type_current_account)
self.driver.click1(el_account_type_current_account_list[1])
else:
css_account_type_saving_account = self.add_merchantR2_config_ini.get_ini_value("add_merchantR2.ini",
"bankaccounts",
"saving_account")
el_account_type_saving_account_list = self.driver.getElenments(css_account_type_saving_account)
self.driver.click1(el_account_type_saving_account_list[1])

python+selenium运行时,提示元素不可见的更多相关文章
- Python+Selenium自动化-定位一组元素,单选框、复选框的选中方法
Python+Selenium自动化-定位一组元素,单选框.复选框的选中方法 之前学习了8种定位单个元素的方法,同时webdriver还提供了8种定位一组元素的方法.唯一区别就是在单词elemen ...
- Python+Selenium自动化-定位页面元素的八种方法
Python+Selenium自动化-定位页面元素的八种方法 本篇文字主要学习selenium定位页面元素的集中方法,以百度首页为例子. 0.元素定位方法主要有: id定位:find_elemen ...
- Python+Selenium定位不到元素常见原因及解决办法(报:NoSuchElementException)
在做web应用的自动化测试时,定位元素是必不可少的,这个过程经常会碰到定位不到元素的情况(报selenium.common.exceptions.NoSuchElementException ...
- Python+selenium(定位一组元素)
我们熟知Webdriver有8种定位元素的方法,但是当需要定位一组元素的时候,可以使用Webdriver提供的与之对应的8种用于定位一组元素的方法,分别是: find_elements_by_id() ...
- 【python+selenium】截取某个元素
一. selenium截图1.selenium提供了几个截取全屏的方法- get_screenshot_as_file(self, filename) --这个方法是获取当前window的截图,出现I ...
- python + selenium相关事件和元素定位
女友由于工作上的失误,将公司RDM中的某一字段的2000条数据给删除了.....就算是重新添加字段,但是与其他数据的关联性已经不在了.由于每天的数据修改量大,有关部门不愿意恢复数据库,因此只能一条条的 ...
- python + selenium 练习篇 - 定位元素的方法
1.利用ID定位元素(能直接通过ID来定位的元素比较少) # coding=utf-8from selenium import webdriverdriver = webdriver.Chrome() ...
- python+selenium简单实现拖动元素实例
from selenium import webdriver#引入ActionChains类from selenium.webdriver.common.action_chains impo ...
- Python+selenium(操作隐藏元素)
测试过程中,偶尔会碰到一些页面的隐藏元素,如下,是小编举的一个简单的例子: test.html <html> <head></head> <body> ...
随机推荐
- C++基础——类继承
一.前言 好吧,本系列博客已经变成了<C++ Primer Plus>的读书笔记,尴尬.在使用C语言时,多通过添加库函数的方式实现代码重用,但有一个弊端就是原来写好的代码并不完全适用于现 ...
- 第八周LINUX学习笔记
vsftpd丶NFS丶SAMBA nfs基于rpcsamba基于cifs(smb) DRBD: ftp:File Transfer protocol 文件传输协议 两个连接: tcp:命 ...
- [.net core] 在 Windows 中运行出现 WinHttpException: The parameter is incorrect
有一个 web 服务一直跑在 docker 中,今天需要在 Windows 上部署一个备份版本,于是,签出源代码,编译,运行.结果抛出 500 ,日志中有如下记录: System.Net.Http.H ...
- 3. [leetcode] Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. Examples Give ...
- dotnet中Stream、string及byte[]的相关操作
string与byte[](UTF-8) //string to byte[] string str = "abc中文"; //0x61 0x62 0x63 0xE4 0xB8 0 ...
- python-图像处理之PIL库的使用
1.先看一段英文代码The Python Imaging Library (PIL) adds image processing capabilities to your Python interpr ...
- [Abp 源码分析]零、文章目录
0.系列文章目录 一.Abp 框架启动流程分析 二.模块系统 三.依赖注入 四.模块配置 五.系统设置 六.工作单元的实现 七.仓储与 Entity Framework Core 八.缓存管理 九.事 ...
- 【Android Studio安装部署系列】七、真机运行项目
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 概述 简单介绍下真机运行项目的操作步骤. 手机连接电脑 将手机通过数据线连接到电脑上,此时电脑会自动下载安装驱动程序.如果没有安装上的话, ...
- Python input保证输入为int类型
t = float(input("t(℃)="))
- Generator和Async
引言 接触过Ajax请求的会遇到过异步调用的问题,为了保证调用顺序的正确性,一般我们会在回调函数中调用,也有用到一些新的解决方案如Promise相关的技术. 在异步编程中,还有一种常用的解决方案,它就 ...