2017-09-15 236 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

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