无法单击selenium中的按钮

时间:2017-09-15 09:26:52

标签: c# selenium

我正在努力点击我的硒中的继续按钮。我尝试使用.click()但它声明元素不可点击。我已经尝试过等待该元素在手边可见,甚至尝试按照本文Debugging "Element is not clickable at point" error中的解决方案进行操作,但没有运气。

有没有人知道为什么这是一个问题?我正在用chrome测试它。

<div class="basket-summary__continue"><button data-href="" data-component="Booking/Navigation/ContinueButton" class="bttn bttn--primary bttn--full-width">
    Continue
</button></div>
 public void ClickContinue()
    {
        Thread.Sleep(10000);
       _driver.FindElement(By.ClassName("basket-summary__continue")).FindElement(By.XPath("/html/body/div[2]/div[4]/div/div[2]/div[1]/div[1]/div[2]/div[2]/div[3]/button")).Click();
    }

P.S我真的不想使用Thread.Sleep而只是暂时使用它来创建一个等待。

感谢

2 个答案:

答案 0 :(得分:0)

您可以使用ExpectedConditions类等待元素可点击 - 即可见并启用 -

请检查以下文档: https://seleniumhq.github.io/selenium/docs/api/dotnet/html/M_OpenQA_Selenium_Support_UI_ExpectedConditions_ElementToBeClickable.htm

答案 1 :(得分:0)

@FindBy(xpath = "//div[@class='basket-summary__continue']/button")
private WebElement button;

By buttonBy = By.xpath("//div[@class='basket-summary__continue']/button");

然后在单击按钮之前创建显示等待Element clickable。

/***
 * An expectation for checking an element is visible and enabled such that you can click it.
 * @param locator - used to find the element
 * @param timeout
 * @return the WebElement once it is located and clickable (visible and enabled)
 */
public WebElement elementToBeClickable(By locator, int timeout) {
    try {
        return getWebDriverFluentWait(timeout)
                .until(ExpectedConditions.elementToBeClickable(locator));
    } catch (Exception e) {
        return null;
    }
}

private Wait<WebDriver> getWebDriverFluentWait(int timeout) {
    return new FluentWait<WebDriver>(driver)
            .withTimeout(timeout, TimeUnit.SECONDS)
            .pollingEvery(1, TimeUnit.SECONDS)
            .ignoring(NoSuchElementException.class);
}

最后,我们可以执行以下功能:

WebElement btnContinue = elementToBeClickable(buttonBy, 10); # wait for element clickable within 10 seconds timeout.
btnContinue.click();

//对不起,我更喜欢Java,但我认为我们对其他语言有相同的解决方案。