惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

有赞技术团队
有赞技术团队
Apple Machine Learning Research
Apple Machine Learning Research
IT之家
IT之家
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
B
Blog RSS Feed
酷 壳 – CoolShell
酷 壳 – CoolShell
人人都是产品经理
人人都是产品经理
Hugging Face - Blog
Hugging Face - Blog
博客园_首页
V
V2EX
aimingoo的专栏
aimingoo的专栏
爱范儿
爱范儿
博客园 - 聂微东
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Stack Overflow Blog
Stack Overflow Blog
罗磊的独立博客
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
MongoDB | Blog
MongoDB | Blog
Jina AI
Jina AI
T
The Blog of Author Tim Ferriss
月光博客
月光博客
云风的 BLOG
云风的 BLOG

博客园 - Chen.HJ

python用正则对字符串进行运算 Nginx安装和遇到过的坑 Redis安装与常用命令 Nginx、Tomcat和Redis配置文件说明 Python读取Excel Python使用paramiko的SFTP get或put整个目录 Linux系统性能监控工具nmon python+selenium个人学习笔记11-登录封装与调用 python+selenium+unittest测试框架4-邮件发送最新测试报告 python+selenium+unittest测试框架3-项目构建和发送邮件 python+selenium+unittest测试框架2-装饰器@classmethod python+selenium+unittest测试框架1-unittest单元测试框架和断言 python+selenium个人学习笔记10-调用JavaScript和截图 python+selenium个人学习笔记9-文件上传和cookie操作 python+selenium个人学习笔记8-获取信息和勾选框 python+selenium个人学习笔记7-警告框处理和下拉框选择 python+selenium个人学习笔记6-元素等待 python+selenium个人学习笔记5-多窗口和多表单切换 python+selenium个人学习笔记4-鼠标和键盘操作
python + selenium 获取标签文本的为空解决办法
Chen.HJ · 2019-05-27 · via 博客园 - Chen.HJ

一、确定元素是否被隐藏

link = driver.find_element(*By_xx, 'value').is_displayed()
print(link)

如果输出结果为False,说明元素被隐藏了。

二、解决方法

1、修改当前定位元素方式方法(修改定位元素方式,或者修改定位元素的路径等),使用is_displayed()方法定位元素结果为True。

  由于webdriver spec的定义,selenium WebDriver只会与课件元素交互,所以获取隐藏元素的文本信息返回为空字符串。

2、通过get_attribute()方法获取元素的文本信息。

  在获取隐藏元素的文本信息时,可以使用get_attribute()方法,通过textContent、innerText、innerHTML等属性获取。

  innerHTML会返回元素内部的HTML,包含所有的HTML标签。

  textContent和innerText置灰得到文本内容,而不会包含HTML标签。textContent是W3C兼容的文字内容属性,但是IE不支持;innerText不是W3C DOM的指定内容,但是FireFox不支持。

from selenium.webdriver.common.by import By
from test_case.common.home import Page


class CloudMainPage(Page):
    username_input = (By.ID, 'username')
    password_input = (By.ID, 'password')
    loging_button = (By.XPATH, '//*[@id="loginDiv"]/div[1]/div[1]/ul/li[4]/div[1]')
    loging_result = (By.XPATH, '//*[@id="tuichuxitong"]/span')

    def user_login(self, username, password):
        """
        用户登录
        :param username: 用户名
        :param password: 密码
        :return:
        """
        self.find_element(*self.username_input).clear()
        self.find_element(*self.username_input).send_keys(username)
        self.find_element(*self.password_input).clear()
        self.find_element(*self.password_input).send_keys(password)
        self.find_element(*self.loging_button).click()

    def login_result(self):
        return self.find_element(*self.loging_result).get_attribute('innerText')