2017-09-15 152 views
0

我正在努力點擊硒中的繼續按鈕。我嘗試使用.click(),但它聲明元素不可點擊。我試過等待元素在手前可見,甚至嘗試按照本文中的解決方案Debugging "Element is not clickable at point" error但沒有運氣。無法點擊硒中的按鈕

有沒有人知道爲什麼這是一個問題?我正在用鉻進行測試。

<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只是使用了它創造的等待。

感謝

+0

你能給我們例外的完整堆棧跟蹤嗎? – Murthi

+1

_driver.FindElement(By.Xpath(// button [@ class ='bttn bttn - primary bttn - full-width']「))。click(); – iamsankalp89

+0

xpath看起來是該問題的主要候選者。現在我會在

回答

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

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

點擊該按鈕之前然後創建一個顯式的等待元素點擊。

/*** 
* 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,但我認爲我們有其他語言的相同的解決方案。

+0

嗨託尼,對不起,我有點ac#noob ,你首先要我創建這第一個:public void ClickContinue() { _driver.FindElement(By.XPath(「// div [@ class ='basket-summary__continue']/button」)); 私人WebElement按鈕; } – BruceyBandit

+0

然後代碼的其餘部分在底下? – BruceyBandit

+0

我已經更新了一些答案中的代碼。希望它有效! –