如何使用 Selenium 查找 HTML <style> 标签

How to find HTML <style> tag using Selenium

我正在尝试检索 HTML 中的 <style> 标记中定义的值,但一直收到无法找到该元素的错误消息

//div[contains(@class,'StageModuleText')]//style

从 RobotFramework 获取此错误

Element '//div[contains(@class,'StageModuleText')]//style' not visible after 30 seconds

这是HTML

<style>
.item_5b8bfb83-375e-4605-8388-01340ea4bc61_7642733d-4309-4e81-8f64-5a6a55f4994a_d21bf969-efd0-46eb-9545-4cbbd550139e a {
                  color: #bc206e
                } 
</style>

我正在尝试获取颜色的值,但我什至找不到该元素。但是当控制台打开时,我可以使用 xpath

在元素选项卡中轻松找到它

编辑: 这是一个更广泛的代码示例:

<div class="editor-outer    StageModuleText_wrapper__3kHNI item_60036ccd-a7a8-41f3-b858-d0db08f31cfa_06fab0b6-0ab8-4000-a162-7d1dc8bf97c2_b5ce6362-949f-4b81-bf9c-9a0ebc46bc63">
    <style>
        .item_60036ccd-a7a8-41f3-b858-d0db08f31cfa_06fab0b6-0ab8-4000-a162-7d1dc8bf97c2_b5ce6362-949f-4b81-bf9c-9a0ebc46bc63 a { 
            color: #44a1a9 
        }
        .editor_item_60036ccd-a7a8-41f3-b858-d0db08f31cfa_06fab0b6-0ab8-4000-a162-7d1dc8bf97c2_b5ce6362-949f-4b81-bf9c-9a0ebc46bc63 * { 
                  line-height: 1.2 !important;
            }
    </style>
    <div class="editor-wrapper editorwrapper_text editor_item_60036ccd-a7a8-41f3-b858-d0db08f31cfa_06fab0b6-0ab8-4000-a162-7d1dc8bf97c2_b5ce6362-949f-4b81-bf9c-9a0ebc46bc63 StageModuleText_editorWrapper__19Z6l">
        <div id="mce_66c9e9a4_8d68-41e5-b662-d7be6a68391c" tabindex="-1" class="mce-content-body" contenteditable="true" style="position: relative;">
            <div class="txtTinyMce-wrapper" style="font-size: 14px; line-height: 28px;" data-mce-style="font-size: 14px; line-height: 28px;">
                <p style="font-size: 14px;" data-mce-style="font-size: 14px;">I'm a new Text block ready for your content.</p>
            </div>
        </div>
    </div>
</div>

使用下面的代码我能够提取颜色值

    WebElement ele = driver.findElement(By.cssSelector("div[class*=editor-outer]"));
    
    String html = ele.getAttribute("innerHTML");
    String[] arr = html.split("\n");

    for(int i=0; i< arr.length; i++)
    {
        if(arr[i].contains("color"))
        {
            String color = arr[i].split(":")[1].trim();
            System.out.println(color);
            
        }
    }