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

推荐订阅源

S
SegmentFault 最新的问题
A
About on SuperTechFans
NISL@THU
NISL@THU
V
Visual Studio Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
B
Blog RSS Feed
Engineering at Meta
Engineering at Meta
GbyAI
GbyAI
美团技术团队
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Google DeepMind News
Google DeepMind News
小众软件
小众软件
博客园 - Franky
罗磊的独立博客
The Cloudflare Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
酷 壳 – CoolShell
酷 壳 – CoolShell
量子位
Hugging Face - Blog
Hugging Face - Blog
云风的 BLOG
云风的 BLOG
MongoDB | Blog
MongoDB | Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
B
Blog
The GitHub Blog
The GitHub Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
P
Proofpoint News Feed
有赞技术团队
有赞技术团队
Hacker News - Newest:
Hacker News - Newest: "LLM"
Cyberwarzone
Cyberwarzone
C
CXSECURITY Database RSS Feed - CXSecurity.com
Project Zero
Project Zero
Security Latest
Security Latest
L
Lohrmann on Cybersecurity
AWS News Blog
AWS News Blog
The Hacker News
The Hacker News
I
Intezer
J
Java Code Geeks
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
P
Privacy International News Feed
月光博客
月光博客
A
Arctic Wolf
Apple Machine Learning Research
Apple Machine Learning Research
D
Darknet – Hacking Tools, Hacker News & Cyber Security
博客园_首页
WordPress大学
WordPress大学
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
T
Tor Project blog
博客园 - 三生石上(FineUI控件)
The Last Watchdog
The Last Watchdog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org

博客园 - 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')