2

I have been trying out Selenium WebDriver using Java. I can easily get some clicks done on buttons with an ID, selecting from dropdownboxes by value and even typing on input fields.

I'm trying to make and automation to browse through different categories on a reports platform and get certain reports downloaded by filling the date_start and date_end. Went well using:

WebElement startDate = driver.findElement(By.id("start_date"));
startDate.sendKeys("25082021");
WebElement endDate = driver.findElement(By.id("end_date"));
endDate.sendKeys("25092021");

The problem is to finally click a download button 'ExcelHtml5' that is part of Datatables.

Javascript inside of PHP:

Inspect code of the Excel button

I have tried getting the click done using By.xpath() and By.cssSelector()

But no luck.. here are the latest rows of code I have tried:

WebDriverWait wait = new WebDriverWait(driver, 10); 
WebElement edit_button = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("button#dt-button.buttons-excel.buttons-html5.btn.btn-success.btn-sm.downloadExcel\"")));
edit_button.click();

and another approach:

WebElement excelButton = driver.findElement(By.xpath("//button[@class='downloadExcel'][@type='button'][contains(text(),'Excel')]"));
excelButton.click();

In less than half an hour I got everything ready except the click of 'Excel'-download button. I've tried hard looking for answers on the web and finally after using about 6 hours on getting this Excel button to be clicked using Selenium WebDriver but with no success. I have tried chrome extensions like XPath Helper and Chropath to get the cssSelector.

Anyone who could help me please?

Update: Problem was caused of a new tab that was opened during the process. After using @cruisepandey code solution I got the reports to download successfully!

Corrected code was:

ArrayList<String> tabs =  new ArrayList<String>(driver.getWindowHandles());
driver.switchTo().window(tabs.get(1));
driver.findElement(By.xpath("//div[@class='dt-buttons']//button[contains(@class,'downloadExcel')]")).click();

2 Answers 2

2

There are 4 ways to click in Selenium.

I will use this xpath

//div[@class='dt-buttons']//button[contains(@class,'downloadExcel')]

Code trial 1 :

time.sleep(5)
driver.find_element_by_xpath("//div[@class='dt-buttons']//button[contains(@class,'downloadExcel')]").click()

Code trial 2 :

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='dt-buttons']//button[contains(@class,'downloadExcel')]"))).click()

Code trial 3 :

time.sleep(5)
button = driver.find_element_by_xpath("//div[@class='dt-buttons']//button[contains(@class,'downloadExcel')]")
driver.execute_script("arguments[0].click();", button)

Code trial 4 :

time.sleep(5)
button = driver.find_element_by_xpath("//div[@class='dt-buttons']//button[contains(@class,'downloadExcel')]")
ActionChains(driver).move_to_element(button).click().perform()

Imports :

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains

PS : Please check in the dev tools if we have unique entry in HTML DOM or not.

Steps to check:

Press F12 in Chrome -> go to element section -> do a CTRL + F -> then paste the xpath and see, if your desired element is getting highlighted.

Update 1 (Java):

There are 4 ways to click in Selenium.

I will use this xpath

//span[text()='Excel']/parent::button[@aria-controls='report'][contains(@class,'downloadExcel')]

Code trial 1 :

Thread.sleep(5);
driver.findElement(By.xpath("//span[text()='Excel']/parent::button[@aria-controls='report'][contains(@class,'downloadExcel')]")).click();

Code trial 2 :

new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[text()='Excel']/parent::button[@aria-controls='report'][contains(@class,'downloadExcel')]"))).click();

Code trial 3 :

Thread.sleep(5);
WebElement button = driver.findElement(By.xpath("//span[text()='Excel']/parent::button[@aria-controls='report'][contains(@class,'downloadExcel')]"));
((JavascriptExecutor)driver).executeScript("arguments[0].click();", button);

Code trial 4 :

Thread.sleep(5);
WebElement button  = driver.findElement(By.xpath("//span[text()='Excel']/parent::button[@aria-controls='report'][contains(@class,'downloadExcel')]"));
new Actions(driver).moveToElement(button).click().build().perform();

Update 2 :

ArrayList<String> tabs =  new ArrayList<String>(driver.getWindowHandles());
driver.switchTo().window(tabs.get(1));
Sign up to request clarification or add additional context in comments.

20 Comments

Your provided xpath //div[@class='dt-buttons']//button[contains(@class,'downloadExcel')] highlights the Excel button nicely. I tried code trial 1 and 2, but it justs sits there and shows a click animation but it doesn't really click the button. I'll go through trials 3-4 and let you know asap! :)
I had no idea it could be a problem, but just now realized that "create report"-button opens a new tab. Deeply sorry for not bringing this up yesterday
@Manny : Not an issue, we learn everyday. See the above update 2 section where I have given you the code to switch to new windows, also you should write this after once you click the create report link.
So the problem was that the new tab was not selected and by using above solution corrected the problem. I'm really happy that this got resolved even I had not given all the information needed. Thank you @cruisepandey for helping me! The report got downloaded and now I just need to close the current tab, select a new report, create report, move to the second tab again and click download :P
@Manny : had that info little early, would have solved the issue yesterday itself. But glad we could work together to close this ticket. Have a good day !
|
1

Try this:

WebDriverWait wait = new WebDriverWait(driver, 10); 
WebElement edit_button = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//button[contains(@class,'downloadExcel')]")));
edit_button.click();

In case //button[contains(@class,'downloadExcel')] XPath is not unique locator try this:

WebDriverWait wait = new WebDriverWait(driver, 10); 
WebElement edit_button = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//button[contains(@class,'downloadExcel') and(contains(@class,'btn-success'))]")));
edit_button.click();

3 Comments

First try: Exception in thread "main" org.openqa.selenium.TimeoutException: Expected condition failed: waiting for visibility of element located by By.xpath: //button[contains(@class,'downloadExcel')] (tried for 10 second(s) with 500 milliseconds interval) The second code gave me this error: Exception in thread "main" org.openqa.selenium.TimeoutException: Expected condition failed: waiting for visibility of element located by By.xpath: //button[contains(@class,'downloadExcel') and(contains(@class,'btn-success'))] (tried for 10 second(s) with 500 milliseconds interval)
Can you share a link to that page?
I wish I could but its an company inside platform :/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.