pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

如何在使用 Selenium Python 之前定位偽元素 ::befor

How locate the pseudo-element ::before using Selenium Python(如何在使用 Selenium Python 之前定位偽元素 ::before)
本文介紹了如何在使用 Selenium Python 之前定位偽元素 ::before的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在使用 Selenium Python 來定位標簽元素.我想使用 ::before 來定位它,因為這是一個彈出窗口.

 <div class="crow" grp="0" grpname="Pizza Size">::前<標簽類="label0" cid="1"><input type="radio" name="0" coname="M" sname="" price="9.99" value="392">M<b class="ip">9.99</b></標簽><label class="label0" cid="1"><input type="radio" name="0" coname="L" sname="" price="11.99" value="393">L<b class="ip">11.99</b></label><div style="clear:both"></div></div>

我不知道如何使用 ::before 來定位它,有朋友可以幫忙嗎?

解決方案

  • 代碼塊:

    從 selenium 導入 webdriver選項 = webdriver.ChromeOptions()options.add_argument("開始最大化")options.add_experimental_option("excludeSwitches", ["enable-automation"])options.add_experimental_option('useAutomationExtension', False)driver = webdriver.Chrome(options=options, executable_path=r'C:WebDriverschromedriver.exe')driver.get('https://meyerweb.com/eric/css/tests/pseudos-inspector-test.html')script = "返回 window.getComputedStyle(document.querySelector('body>p.el'),':after').getPropertyValue('content')"打印(driver.execute_script(腳本).strip())

  • 控制臺輸出:

    " (fin.)"

此控制臺輸出與 ::aftercontent 屬性的 value 完全匹配

<小時>

這個用例

要提取 ::before 元素的 content 屬性的值,您可以使用以下解決方案:

script = "return window.getComputedStyle(document.querySelector('div.crow'),':before').getPropertyValue('content')"打印(driver.execute_script(腳本).strip())

<小時>

結尾

一些相關文件:

  • Document.querySelector()
  • Window.getComputedStyle()

I'm using Selenium Python to locate label element.I want to use ::before to locate it,because this is a pop window.

 <div class="crow" grp="0" grpname="Pizza Size">
    ::before
    <label class="label0" cid="1">
    <input type="radio" name="0" coname="M" sname="" price="9.99" value="392">M<b class="ip">9.99</b>
    </label>
    <label class="label0" cid="1"><input type="radio" name="0" coname="L" sname="" price="11.99" value="393">L<b class="ip">11.99</b>
    </label><div style="clear:both">
    </div>
    </div>

I have no idea how to use ::before to locate it,any friend can help?

解決方案

Pseudo Elements

A CSS pseudo-element is used to style specified parts of an element. It can be used to:

  • Style the first letter, or line, of an element
  • Insert content before, or after, the content of an element

::after

::after is a pseudo element which allows you to insert content onto a page from CSS (without it needing to be in the HTML). While the end result is not actually in the DOM, it appears on the page as if it is, and would essentially be like this:

CSS:

div::after {
  content: "hi";
}


::before

::before is exactly the same only it inserts the content before any other content in the HTML instead of after. The only reasons to use one over the other are:

  • You want the generated content to come before the element content, positionally.
  • The ::after content is also "after" in source-order, so it will position on top of ::before if stacked on top of each other naturally.

Demonstration of extracting properties of pseudo-element

As per the discussion above you can't locate the ::before element within the DOM Tree but you can always be able to retrieve the contents of the pseudo-elements, i.e. ::before and ::after elements. Here's an example:

To demonstrate, we will be extracting the content of ::after element (snapshot below) within this website:

  • Code Block:

    from selenium import webdriver
    
    options = webdriver.ChromeOptions() 
    options.add_argument("start-maximized")
    options.add_experimental_option("excludeSwitches", ["enable-automation"])
    options.add_experimental_option('useAutomationExtension', False)
    driver = webdriver.Chrome(options=options, executable_path=r'C:WebDriverschromedriver.exe')
    driver.get('https://meyerweb.com/eric/css/tests/pseudos-inspector-test.html')
    script = "return window.getComputedStyle(document.querySelector('body>p.el'),':after').getPropertyValue('content')"
    print(driver.execute_script(script).strip())
    

  • Console Output:

    " (fin.)"
    

This console output exactly matches the value of the content property of the ::after element as seen in the HTML DOM:


This usecase

To extract the value of the content property of the ::before element you can use the following solution:

script = "return window.getComputedStyle(document.querySelector('div.crow'),':before').getPropertyValue('content')"
print(driver.execute_script(script).strip())


Outro

A couple of relevant documentations:

  • Document.querySelector()
  • Window.getComputedStyle()

這篇關于如何在使用 Selenium Python 之前定位偽元素 ::before的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區域周圍繪制一個矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測和跟蹤人員?)
How to apply threshold within multiple rectangular bounding boxes in an image?(如何在圖像的多個矩形邊界框中應用閾值?)
How can I download a specific part of Coco Dataset?(如何下載 Coco Dataset 的特定部分?)
Detect image orientation angle based on text direction(根據文本方向檢測圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 挨踢网-大家的导航!| 喷涂流水线,涂装流水线,喷漆流水线-山东天意设备科技有限公司 | 金属抛光机-磁悬浮抛光机-磁力研磨机-磁力清洗机 - 苏州冠古科技 | 玻璃瓶厂家_酱菜瓶厂家_饮料瓶厂家_酒瓶厂家_玻璃杯厂家_徐州东明玻璃制品有限公司 | 丁基胶边来料加工,医用活塞边角料加工,异戊二烯橡胶边来料加工-河北盛唐橡胶制品有限公司 | 档案密集架,移动密集架,手摇式密集架,吉林档案密集架-厂家直销★价格公道★质量保证 | 焊管生产线_焊管机组_轧辊模具_焊管设备_焊管设备厂家_石家庄翔昱机械 | 泰安塞纳春天装饰公司【网站】 | 电车线(用于供电给电车的输电线路)-百科| 优考试_免费在线考试系统_培训考试系统_题库系统_组卷答题系统_匡优考试 | T恤衫定做,企业文化衫制作订做,广告T恤POLO衫定制厂家[源头工厂]-【汉诚T恤定制网】 | 铝箔袋,铝箔袋厂家,东莞铝箔袋,防静电铝箔袋,防静电屏蔽袋,防静电真空袋,真空袋-东莞铭晋让您的产品与众不同 | 坏男孩影院-提供最新电影_动漫_综艺_电视剧_迅雷免费电影最新观看 | 山东彩钢板房,山东彩钢活动房,临沂彩钢房-临沂市贵通钢结构工程有限公司 | 全自动固相萃取仪_高通量真空平行浓缩仪-勤业永为 | 考勤系统_人事考勤管理系统_本地部署BS考勤系统_考勤软件_天时考勤管理专家 | 北京乾茂兴业科技发展有限公司 | 捆扎机_气动捆扎机_钢带捆扎机-沈阳海鹞气动钢带捆扎机公司 | 加盟店-品牌招商加盟-创业项目商机平台 | 电子厂招聘_工厂招聘_普工招聘_小时工招聘信息平台-众立方招工网 | 超声波清洗机_大型超声波清洗机_工业超声波清洗设备-洁盟清洗设备 | 单电机制砂机,BHS制砂机,制沙机设备,制砂机价格-正升制砂机厂家 单级/双级旋片式真空泵厂家,2xz旋片真空泵-浙江台州求精真空泵有限公司 | 圆窗水平仪|伊莉莎冈特elesa+ganter| 济南品牌包装设计公司_济南VI标志设计公司_山东锐尚文化传播 | 展厅装修公司|企业展厅设计|展厅制作|展厅搭建—广州展厅装饰公司 | 中国产业发展研究网 - 提供行业研究报告 可行性研究报告 投资咨询 市场调研服务 | 嘉兴恒升声级计-湖南衡仪声级计-杭州爱华多功能声级计-上海邦沃仪器设备有限公司 | 南京租车,南京汽车租赁,南京包车,南京会议租车-南京七熹租车 | 压力控制器,差压控制器,温度控制器,防爆压力控制器,防爆温度控制器,防爆差压控制器-常州天利智能控制股份有限公司 | 全温度恒温培养摇床-大容量-立式-远红外二氧化碳培养箱|南荣百科 | 阿里巴巴诚信通温州、台州、宁波、嘉兴授权渠道商-浙江联欣科技提供阿里会员办理 | 青岛代理记账_青岛李沧代理记账公司_青岛崂山代理记账一个月多少钱_青岛德辉财税事务所官网 | 防爆电机-高压防爆电机-ybx4电动机厂家-河南省南洋防爆电机有限公司 | 耐驰泵阀管件制造-耐驰泵阀科技(天津)有限公司 | 奇酷教育-Python培训|UI培训|WEB大前端培训|Unity3D培训|HTML5培训|人工智能培训|JAVA开发的教育品牌 | 对夹式止回阀厂家,温州对夹式止回阀制造商--永嘉县润丰阀门有限公司 | 河南生物显微镜,全自动冰冻切片机-河南荣程联合科技有限公司 | 高压绝缘垫-红色配电房绝缘垫-绿色高压绝缘地毯-上海苏海电气 | 新材料分散-高速均质搅拌机-超声波分散混合-上海化烁智能设备有限公司 | 冷镦机-多工位冷镦机-高速冷镦机厂家-温州金诺机械设备制造有限公司 | 微信小程序定制,广州app公众号商城网站开发公司-广东锋火 |