K2017
K2017

Reputation: 53

java selenium element found but one click does not work

I have a piece of selenium code to look for a certain element and afterwards to click on this same element. The element is found, but after that, the click does not seem to do the trick. Here is the code for finding the element (unfortunately using xpaths, because there are no id's and it uses a lot of selfmade methods):

String taakButton = "Start taak button";
String xpathButton = "//td[contains(text(),'" + datumhelper.formateerDatumVoorFlowAanmaakdatum(SoapHelper.datumVoorAanvraag5SpaarrekeningSns) + "')]/following-sibling::*[4]/button";
WebElement startButton = selenium.searchForElementByXpathWithoutSwitchToFrame(xpathButton, taakButton);

I use the above String xpathButton to store a WebElement in a variable, after that I pass the WebElement to a method in a different class: The searchForElementByXpathWithoutSwitchToFrame looks for the element and verifies if it is found.

WebElement startTaakButton = selenium.searchForElementByXpathWithoutSwitchToFrame(xpathStartTaakButton, taakButton);

The element is found, now click on it :

selenium.klikStartOfInzienTaak(startTaakButton, xpathStartTaakButton);

The klikStartOfInzienTaak method, which performs the click looks like this:

public void klikStartOfInzienTaak(WebElement webElementToBeClicked, String xpathToBeClicked) throws InterruptedException {
Actions action = new Actions(driver);
//check to see if element is not null
Assert.assertNotNull("WebElement 'startOfInzienTaak' niet gevonden", webElementToBeClicked);
//Thread.sleep(2500);
//I use Action doubleClick in the hope that would work.       
action.moveToElement(webElementToBeClicked).doubleClick().build().perform();

I also used regular driver.click(). It seems the element is found because it does not give a NoSuchElementException and I see the focus is on the element to be clicked, but nothing happens: Button to be clicked

When I uncomment the Thread.sleep it works, but I don't want to be using Thread.sleep.

As you can see in the image below with the loading image, the page seems to be (re)loading again after the element was already found and clicked on. Thats why the Thread.sleep works: Button found and clicked but page (re)loading

Does anybody know what to do in order for me to remove the thread.sleep? Selenium has to somehow wait again for the page to reload again although the element is already found?

Upvotes: 2

Views: 2146

Answers (2)

Anshul Srivastava
Anshul Srivastava

Reputation: 37

You can use WebDriverWait function to first find the element in the webpage and then can click on the element. By this i didn't had to use thread.sleep.

WebDriverWait for_element = new WebDriverWait(20, TimeUnit.SECONDS);
for_element.until(ExpectedConditions.elementToBeClickable(driver.findElement(by what means u have to find the path);
dr.findElement().click();

Hope that this works for you as it worked for me!

Upvotes: 0

Amit Gurbani
Amit Gurbani

Reputation: 607

Try JavascriptExecutor to click the element,

Refer code,

WebElement element = driver.findElement(By.xpath(xpathButton));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);

Upvotes: 1

Related Questions