【问题标题】:How to get value from a particular web element's CSS property using BEHAT/MINK (getCssValue) PHP如何使用 BEHAT/MINK (getCssValue) PHP 从特定 Web 元素的 CSS 属性中获取值
【发布时间】:2015-09-02 13:23:38
【问题描述】:

我正在练习使用 cssGetValue 方法从特定 Web 元素的 CSS 属性中检索值。如何从 Web 元素中获取 CSS 值? 这是我的 HTML 代码:

<div class="odd content-grid-0 content-grid-item">
<div class="even content-grid-1 content-grid-item impression-pixel-processed">
<div class="odd content-grid-2 content-grid-item">
<div class="even content-grid-3 content-grid-item">
<div class="odd content-grid-4 content-grid-item">
<div class="even content-grid-5 content-grid-item">
<div class="odd content-grid-6 content-grid-item">
<div class="even content-grid-7 content-grid-item">
<div class="odd content-grid-8 content-grid-item">

我需要为每个 DIV 获取 CSS 值 'clear'

clear = left

【问题讨论】:

    标签: php behat mink getvalue


    【解决方案1】:

    你可以试试这个:

     /**
         * @Then /^I should see "([^"]*)" in the "([^"]*)" attribute of the "([^"]*)" element$/
         */
        public
        function iShouldSeeInTheAttributeOfTheElement($content, $attribute, $elementCSS)
        {
            $page = $this->getSession()->getPage();
            $element = $page->find("css", $elementCSS);
            if (!$element) {
                throw new Exception($elementCSS . ' does not exist');
            }
    
    
            $attributeValue = $element->getAttribute($attribute);
    
            if (!$attributeValue) {
                throw new Exception($elementCSS . ' does not have an "' . $attribute . '" attribute');
            } else if (!is_numeric(strpos($attributeValue, $content))) {
                throw new Exception('The "' . $attribute . '" attribute for ' . $elementCSS . ' does not contain "' . $content . '"');
            }
    
        }
    

    场景中的布局示例如下:

    Then I should see "left" in the "clear" attribute of the ".content-grid-0" element
    

    我希望这会有所帮助。

    【讨论】:

    • 嗨,Kyle,getAttribute 可以获取仅存在于 HTML 中的属性,但我需要从 css 文件中获取它。我在我的问题中添加了部分 HTML 代码。我有
      并且需要从 css 文件中获取 cssValue。
    • @IhorBovkit 你找到解决这个问题的方法了吗?谢谢!
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签