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.
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();
CodePudding user response:
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));
CodePudding user response:
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();