You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Watir Selenium日历日期背景色校验问题:无法获取日期颜色

解决Watir无法获取日历日期背景色的问题

以下是导致你无法获取背景色的核心问题及对应解决方案:

1. 方法内实例变量与参数混用

你定义check_attendance_for_month时接收了browser参数,但方法内部却使用@browser,这会导致上下文错误,需统一使用参数或确保@browser为全局可用的实例变量。

2. RGB颜色解析正则覆盖不全

部分浏览器会返回带透明度的rgba格式(比如rgba(0,128,0,1)),你的正则未匹配这种情况,同时也未处理十六进制颜色的可能场景,需要调整正则逻辑。

3. 背景色可能不在td元素本身

多数日历组件的背景色会设置在td内部的子元素(如<div>或<span>)上,而非td标签本身,需确认实际承载背景色的元素。

修正后的完整代码

# 移除未使用的require 'color'

def get_current_month_and_year_from_calendar(browser)  
  calendar_title_element = browser.span(class: 'calendar-title')
  calendar_title = calendar_title_element.text
  month, year = calendar_title.split(' ')
  [month, year.to_i]
end

# 替换绝对XPath为更健壮的类定位(可根据实际DOM调整)
backward_button = @browser.link(class: 'calendar-prev')

current_month, current_year = get_current_month_and_year_from_calendar(@browser)
target_month = "January"
target_year = 2024

# 用显式等待代替硬sleep,提升可靠性
while !(current_month == target_month && current_year == target_year)
  backward_button.click
  # 等待日历标题更新后再获取新月份
  @browser.span(class: 'calendar-title').wait_until(&:present?)
  current_month, current_year = get_current_month_and_year_from_calendar(@browser)
end

def check_attendance_for_month(browser)
  # 统一使用传入的browser参数
  month_calendar = browser.elements(xpath: "//table[@class='simple-calendar']//td[contains(@class,'current-month')]")
  
  month_calendar.each do |day|
    # 优先获取td内子元素的背景色,若不存在则使用td本身(可根据实际DOM调整)
    color_element = day.element(tag_name: 'div').present? ? day.element(tag_name: 'div') : day
    color = get_color_for_day(color_element)
    
    if day_is_present?(color)
      puts "Day #{day.text} is present"
    elsif day_is_Work_From_Home?(color)
      puts "Day #{day.text} is Work From Home"
    elsif day_is_CompOff_Present?(color)
      puts "Day #{day.text} is CompOff and Present"
    else
      puts "Day #{day.text} has an unknown color: #{color.inspect}"
    end
  end
end

def get_color_for_day(day_element)
  color_style = day_element.style('background-color')
  parse_color(color_style)
end

def parse_color(color_style)
  # 兼容rgb和rgba格式,忽略透明度值
  rgb_match = color_style.match(/\Argba?\((\d+),\s*(\d+),\s*(\d+)/)
  if rgb_match
    red = rgb_match[1].to_i
    green = rgb_match[2].to_i
    blue = rgb_match[3].to_i
    return [red, green, blue]
  end
  
  # 处理十六进制颜色格式
  hex_match = color_style.match(/\A#([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})\z/)
  if hex_match
    red = hex_match[1].to_i(16)
    green = hex_match[2].to_i(16)
    blue = hex_match[3].to_i(16)
    return [red, green, blue]
  end
  nil
end

def day_is_present?(color)
  present_color = [0, 128, 0]  # Green
  compare_colors(color, present_color)
end

def day_is_CompOff_Present?(color)
  compOff_present_color = [255, 165, 0]  # Orange
  compare_colors(color, present_color)
end

def day_is_Work_From_Home?(color)
  work_from_home_color = [0, 0, 255]  # Blue
  compare_colors(color, present_color)
end

def compare_colors(color, target_color)
  color && color == target_color
end

check_attendance_for_month(@browser)

额外调试建议

  • 打开浏览器开发者工具,检查目标日期元素的background-color属性实际绑定在哪个标签上,确保代码定位的是正确元素。
  • 在get_color_for_day方法中添加puts color_style,查看实际返回的颜色字符串格式,以便进一步调整解析逻辑。

内容的提问来源于stack exchange,提问作者spd

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 21:26:32