【问题标题】:How to find HTML <style> tag using Selenium如何使用 Selenium 查找 HTML <style> 标签
【发布时间】:2021-10-20 01:40:32
【问题描述】:

我正在尝试检索 HTML 中 &lt;style&gt; 标记中定义的值,但不断收到无法找到该元素的错误

//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>

【问题讨论】:

  • not visible after 30 seconds 并不表示“元素无法找到”,而是表示它不可见。请注意,style 节点在任何情况下都不会可见
  • 你能分享相关的HTML或页面URL吗?
  • 为什么不直接使用 By.tagName("style")
  • @Kundan : 因为 HTML 中可能有多个样式标签,而 OP 肯定不想要第一个样式标签。
  • @cruisepandey 我的意思是将 div 元素存储在一个变量中,然后在该变量上使用 findElement(By.tagName()) 。抱歉,我的第一条评论不清楚。或者,你不能只获取 div 标签的文本并从中提取颜色值吗?

标签: css selenium xpath automation robotframework


【解决方案1】:

通过以下代码,我能够提取颜色值

    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);
            
        }
    }

【讨论】:

  • 谢谢!在 python/RobotFramework 中实现了类似的东西。这是任何人的代码 font_color = "css:div[class*=editor-outer]" element_style = self.selib.get_element_attribute(font_color, "innerHTML") hex_value = re.search("color: (#.{6} )", element_style).group(1).split(',')[0]
猜你喜欢
  • 1970-01-01
  • 2021-01-25
  • 2023-02-15
  • 2023-03-26
  • 2016-03-22
  • 2012-04-22
  • 1970-01-01
  • 2018-07-21
  • 1970-01-01
相关资源
最近更新 更多