0

Why doesn't my wait method click on the intended button?

    public void waitAndClickElement(WebElement element) throws InterruptedException {
    this.wait = new WebDriverWait(driver, 30);
    Boolean elementPresent = wait.until(ExpectedConditions.elementToBeClickable(element)).isEnabled();
    try {
        if(elementPresent==true){
            wait.until(ExpectedConditions.visibilityOf(element)).click();
            //clickElementUsingJS(element);
        }else {
            System.out.println("The specified element cannot be found: " + element);
        }
    } catch(Exception e) {
        throw(e);
    }
}

If I use Thread.sleep infront of the method it seems to click on the button:

    @Test(priority=4) 
public void clickOnFirstSuperCarOption() throws Exception 
{
    Thread.sleep(2000);
    basePage.waitAndClickElement(supercarsPage.link_FirstSupercarOption);
}

enter image description here

Locator:

public @FindBy(xpath=".//*[@id='prd_listing']/div/li[1]/a") WebElement link_FirstSupercarOption;
Panup Pong
  • 1,871
  • 2
  • 22
  • 44
Gbru
  • 1,065
  • 3
  • 24
  • 56
  • Which exception are you getting? – acikojevic Jan 17 '17 at 13:57
  • @acikojevic this is an example exception which im getting: org.openqa.selenium.TimeoutException: Expected condition failed: waiting for element to be clickable: By.xpath: .//*[@id='prd_pg_rgt']/div[2]/form/div/input (tried for 30 second(s) with 500 MILLISECONDS interval) – Gbru Jan 17 '17 at 14:13
  • The element `By.xpath: .//*[@id='prd_pg_rgt']/div[2]/form/div/input` cannot be found. Try to find out what that element is. I don't see you mention it in the question – Linh Nguyen Jan 17 '17 at 14:31
  • @Linh Nguyen the element is a strong link, it seems to work when running some test cases but not on others, think its wise just to add a Thread.sleep inside the method; doing this makes the method work – Gbru Jan 17 '17 at 14:44
  • Could you show the html part where it contains `id=prd_pg_rgt`, and the code where you look for the element `By.xpath: .//*[@id='prd_pg_rgt']/div[2]/form/div/input `? – Linh Nguyen Jan 17 '17 at 14:47
  • Please refer to my answer [HERE](http://stackoverflow.com/questions/32490572/chromedriver-element-is-not-clickable-at-point-xxx-yyy-other-element-would-r/41740240#41740240); it might help you. – Kushal Bhalaik Jan 20 '17 at 11:53
  • Please refer to my answer [HERE](http://stackoverflow.com/questions/32490572/chromedriver-element-is-not-clickable-at-point-xxx-yyy-other-element-would-r/41740240#41740240); it might help you. – Kushal Bhalaik Jan 20 '17 at 11:55

0 Answers0