如果元素不可点击,为什么 find(element).click 使用 max_wait_time 而不是指定的 wait:0?

Why find(element).click uses max_wait_time instead of the specified wait:0 if the element is not clickable?

背景:检查元素是否可点击(我希望它不可点击,我想断言这一点)而不等待。 find().click 使用 Capybara.max_wait_time 然后 returns exception:

Selenium::WebDriver::Error::UnknownError Exception: unknown error: Element <input type="text" class="form-control floatingInput" name="firstname" placeholder="Enter your first name" aria-label="First Name" value="Lynn"> is not clickable at point (415, 301). Other element would receive the click: <div class="Checkout__overlay__1cqyM"></div>

而不是立即返回异常。

在我看来不应该等待的确切代码行。如果元素不可点击(按预期被覆盖隐藏),这将等待 max_wait_time(例如 20 秒)。很明显我提供了 wait: 0

find(checkout.ship_first_name.path, wait: 0).click

似乎有不同的异常超时,或者 #click 使用它自己的超时,但是 #click 不接受参数 AFAIK。

注一

find(element, wait: 0) 本身可以正常工作,因为我在 if/else 块中使用它,并且没有像指定的那样不必要的等待。

注2

#visible? 在这种情况下不起作用,因为该元素是可见的,但在透明的灰色覆盖层后面。我需要验证它不可点击。

click 确实有一些选项(点击时按住修改键,点击偏移,wait/retry 时间)所以做你想做的是

find(checkout.ship_first_name.path, wait: 0).click(wait: 0)

using_wait_time(0) do # may need to be page.using_wait_time(0) depending on your setup
  find(checkout.ship_first_name.path).click
end

然而,从 Capybara 3.20+(假设您使用的是 Chrome 或 Firefox)开始,您也可以这样做

find(checkout.ship_first_name.path).obscured?

查看它是否在页面上但被遮挡而无法被点击。如果您期望它应该被遮挡,那么您会做

expect(page).to have_css(checkout.ship_first_name.path, obscured: true)