Python:Selenium Chrome无弹窗+property/attribute/text
我们在用Selenium写自动化程序时候,并不希望程序在实际运行过程中一直弹Chrome窗口,这个时候就需要让Chrome默默打开,自动读取数据,然后默默关闭掉就好。
- 以下是让chrome用无界面形式打开方法,主要是chrome_options参数的设置。在使用过程中发现如果chrome浏览器版本是v60+的会不起作用,升级到v70+就可以了。
#让chrome用无界面形式打开
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
driver_chrome = webdriver.Chrome(chrome_options=chrome_options)
- 针对text/get_attribute/get_property这3这的区别:
- text是元素本身的文字内容
- get_attribute是该元素的属性,或者说是按钮或者是栏位的title
- get_property是文本框内输入的内容

代码:
from selenium import webdriver url = 'https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd=driver_chrome.close()%20driver_chrome.quit()&oq=%25E5%2586%2599%25E6%258A%2580%25E6%259C%25AF%25E6%2596%2587%25E7%25AB%25A0%25E5%258E%25BB%25E5%2593%25AA%25E9%2587%258C%25E5%25A5%25BD&rsv_pq=b7b407270017fc3f&rsv_t=b631%2B%2Fcdu8anq1OMvmgCcdPCnsCNNe%2BKKawHK4cgDCDH11XkD1wTbuFFLNc&rqlang=cn&rsv_enter=1&inputT=12858&rsv_n=2&rsv_sug3=40&bs=%E5%86%99%E6%8A%80%E6%9C%AF%E6%96%87%E7%AB%A0%E5%8E%BB%E5%93%AA%E9%87%8C%E5%A5%BD' #让chrome用无界面形式打开
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
driver_chrome = webdriver.Chrome(chrome_options=chrome_options) driver_chrome.get(url) #获取text值
text_value = driver_chrome.find_element_by_xpath('''//*[@id="1"]/h3/a''').text
print('text: ' + text_value)
#获取get_attribute值
get_attribute_value = driver_chrome.find_element_by_xpath('''//*[@id="su"]''').get_attribute('value')
print('get_attribute: ' + get_attribute_value)
#获取get_property值
get_property_value = driver_chrome.find_element_by_xpath('''//*[@id="kw"]''').get_property('value')
print('get_property: ' + get_property_value) #关闭web driver
driver_chrome.close()
driver_chrome.quit()
Python:Selenium Chrome无弹窗+property/attribute/text的更多相关文章
- Python selenium chrome 环境配置
Python selenium chrome 环境配置 一.参考文章: 1. 记录一下python easy_install和pip安装地址和方法 http://heipark.iteye.com/b ...
- python+selenium+Chrome options参数
python+selenium+Chrome options参数 Chrome Options常用的行为一般有以下几种: 禁止图片和视频的加载:提升网页加载速度. 添加代理:用于翻墙访问某些页面,或者 ...
- Docker环境下运行python+selenium+chrome
Docker环境下运行python+selenium+chrome docker运行时占用的资源非常少,而且能将环境进行有效的隔离,可以快速的进行部署,因此可以将docker与selenium结合实现 ...
- Python selenium chrome打包exe后禁用控制台输出滚动日志
Python selenium chrome打包exe后,在运行的过程中,如果遇到需要input()输入时,会发现被不断滚动刷新的日志把命令行输入快速顶掉了,通过查阅资料不断实践,发现以下方法有效: ...
- Python + Selenium +Chrome 批量下载网页代码修改【新手必学】
Python + Selenium +Chrome 批量下载网页代码修改主要修改以下代码可以调用 本地的 user-agent.txt 和 cookie.txt来达到在登陆状态下 批量打开并下载网页, ...
- chrome浏览器爬虫WebDriverException解决采用python + selenium + chrome + headless模式
WebDriverException: Message: unknown error: Chrome failed to start: crashed 第一种:如果出现下面情况: chrome浏览器有 ...
- python selenium+phantomjs alert()弹窗报错
问题:用selenium+phantomjs 模拟登陆,网页用JavaScript的alert("登陆成功")弹出框,但是用switch_to_alert().accept()报错 ...
- Python+Selenium+Chrome 的一个案例
第一步,下载chromeDrive:http://npm.taobao.org/mirrors/chromedriver(我下载的是2.43版本的chromedriver_win32.zip) 下载之 ...
- 在Centos7上安装Python+Selenium+Chrome+Chromedriver
1.下载Chrome 上一篇文章已经演示过了Python+Selenium+Firefox+Geckodriver安装步骤并通过自动化脚本打开百度 因此当前只需要安装Chrome和Chromedriv ...
随机推荐
- 20-java 对象链表空没空呢
写了一个 对象链表,往里面add了一些对象,最后我想看下链表是否为空,用 == null 为假,也看不出, 看下长度? 好吧, size() = 1: 打印 null , 那到底是不是空 啊, ...
- discuz回贴通知插件实现-获取邮件内容
//自定义钩子函数,命名:模块_函数名_output()或模块_函数名() //一个是模块执行完,模板输出前执行.一个是模块执行前 //post_reply_output函数会在所有post操作中都会 ...
- adf常用方法总结
1.使用clientAttribute传值.获取值 或组件上面放客户端属性 <af:selectBooleanCheckbox text="" label="&qu ...
- git pull和git fetch命令
git pull和git fetch命令 git pull git pull命令的作用是取回远程主机某个分支的更新,在与本地指定分支合并,格式如下: $ git pull <远程主机名>& ...
- JTemplate学习(二)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DT ...
- Halcon的二维码解码步骤和解码技巧
一.二维码简介 1 . 类型多样,常见的有QR Code二维码. Data Matrix二维码等. 2.高密度编码,信息容量大. 3.容错能力强,具有纠错功能:二维码因穿孔.污损等引起局部损坏时,照样 ...
- loadrunner12.5-vugen回放脚本提示:URL=“http://www.testclass.net/js/scripts.js”的常规连接当前无套接字 (16 不足) 可用,是什么意思呢?怎么理解呢?
会发生这个报错,是因为每个浏览器都有一个限制,检查哪个浏览器客户正在模拟, 通常只允许16个并发连接. 如果超过此超过接数,将显示该消息,通知您没有可用的连接. 而max connection的默认值 ...
- Codeforces 665A. Buses Between Cities 模拟
A. Buses Between Cities time limit per test: 1 second memory limit per test: 256 megabytes input: s ...
- source Insight工程的简单使用
本文以管理虚拟机里面的uboot为例: 1.选择project->New project->选择工程路径,假设为D:\uboot:->project has its own conf ...
- C++11的一般概念——The C++ standard library, 2nd Edition 笔记(二)
( 原书第四章,General Concepts) 只介绍新内容,关于头文件格式和后缀等C++03已经规范化的内容,不再赘述. namespace std:新的std子空间包括: std::tr1, ...