无法使用 selenium 单击可用日期 ruby

Unable to click on available date using selenium ruby

我无法点击日历中的可用日期。过去的日期显示为灰色,因此尝试获取今天的日期并单击它。我已尝试执行脚本、click() 和 perform(),但其中 none 有效。

today_date =  Date.today.strftime('%d')
element = @driver.find_element(:xpath,"//td[contains(@class,\"CalendarDay__default\")][contains(@aria-label, '#{today_date}')]")
#@driver.execute_script("arguments[0].click;", element )
@driver.action.move_to(ele).click(ele).perform

我也试过循环,但是 td 元素没有显示,因为一些元素是灰色的。不确定如何 select 显示元素?

today_date=  Date.today.strftime('%d')
date_picker= @driver.find_element(:xpath,"//*[contains(@class,'SingleDatePicker_picker')]")
columns=date_picker.find_elements(:tag_name, "td")
calendar_date=columns.map(&:text).reject(&:empty?)
columns.each do |col|
  # This returns true  
  puts "include date: #{calendar_date.include?today_date}"

  if calendar_date.include?today_date
    # Elemement is not displayed
    puts "td displayed: #{col.displayed?}"
    # Not clickable
   col.click
  end
end

请在下面查找 html。

在无法使用实际 html 的情况下,很难知道什么能准确工作,但我的猜测是,该定位器首先匹配的不是您想要的。试试这个:

element = browser.td(aria_label: Time.now.strftime("%A, %B %d, %Y"))

或者您可以 select 第一个非禁用元素而不考虑日期:

element = browser.td(aria_disabled: 'false')

编辑:刚刚意识到您的代码是 Selenium,尽管标签是 Watir。等价于上面的 XPath 是:

".//td[@aria-label=#{Time.now.strftime("%A, %B %d, %Y")}]"    
".//td[@aria-disabled='false']"