【问题标题】:How to check if a pseudo-element ::after exists or not using Selenium Python如何使用 Selenium Python 检查伪元素 ::after 是否存在
【发布时间】:2021-07-19 06:37:23
【问题描述】:

我需要确定 ::after 伪元素是否存在。 当它存在时:

<div class="vcp-bigplay" xpath="1"></div>
  ::after
</div>

当它不存在时:

<div class="vcp-bigplay" xpath="1"></div>

我试过了

driver.find_element_by_css_selector('div.vcp-bigplay>div')

driver.find_element_by_css_selector('div.vcp-bigplay::after')

driver.find_element_by_css_selector('div.vcp-bigplay:after')

它们都不起作用。有人可以帮忙吗?

【问题讨论】:

    标签: selenium-chromedriver pseudo-element


    【解决方案1】:

    您可以使用 JavaScript 执行器来检索内容值,然后检查它是否存在。

    类似的东西

    script = "return window.getComputedStyle(document.querySelector('.vcp-bigplay'),'::after').getPropertyValue('content')";
     
                element = driver.execute_script(script);
                if (len(element)==0):
                     print('not there')
                else:
                     print('there')
    

    您可以详细阅读here

    【讨论】:

    • 无论 ::after 是否存在,它都会一直打印 'there'
    猜你喜欢
    • 2012-03-22
    • 2018-01-23
    • 2021-05-17
    • 2016-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多