Python:Selenium Chrome无弹窗+property/attribute/text

编程入门 行业动态 更新时间:2024-10-24 01:52:28

Python:Selenium <a href=https://www.elefans.com/category/jswz/34/1771100.html style=Chrome无弹窗+property/attribute/text"/>

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是文本框内输入的内容

代码:

在学习过程中有什么不懂得可以加
我的python学习交流扣扣qun,688244617
群里有不错的学习教程、开发工具与电子书籍。
与你分享python企业当下人才需求及怎么从零基础学习好python,和学习什么内容。from selenium import webdriverurl = '=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

本文发布于:2023-07-28 22:05:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1335898.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Chrome   Selenium   Python   无弹窗   text

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!