首页 > 解决方案 > 无法使用 Selenium webdriver Java 单击按钮

问题描述

我一直在尝试使用 Java 的 Selenium WebDriver。我可以轻松地点击带有 ID 的按钮,从下拉框中按值进行选择,甚至在输入字段中输入内容。

我正在尝试自动浏览报告平台上的不同类别,并通过填写 date_start 和 date_end 来下载某些报告。使用顺利:

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

问题是最终单击作为数据表一部分的下载按钮“ExcelHtml5”。

PHP中的Javascript:

检查 Excel 按钮的代码

我尝试使用 By.xpath() 和 By.cssSelector() 完成点击

但没有运气..这是我尝试过的最新代码行:

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();

和另一种方法:

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

在不到半小时的时间里,除了点击“Excel”下载按钮之外,我已经准备好了一切。我已经努力在网上寻找答案,最后在使用 Selenium WebDriver 使用大约 6 个小时来点击这个 Excel 按钮但没有成功之后。我已经尝试过像 XPath Helper 和 Chropath 这样的 chrome 扩展来获取 cssSelector。

任何人都可以帮助我吗?

更新:问题是由在此过程中打开的新选项卡引起的。使用@cruisepandey 代码解决方案后,我成功下载了报告!

更正的代码是:

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();

标签: javaseleniumxpath

解决方案


在 Selenium 中有 4 种点击方式。

我将使用这个 xpath

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

代码试用 1:

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

代码试用 2:

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

代码试用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)

代码试用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()

进口:

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:请检查dev tools我们是否在 HTML DOM 中有唯一条目。

检查步骤:

Press F12 in Chrome-> go to element section-> do a CTRL + F-> 然后粘贴xpath并查看,如果您想要的元素被突出显示。

更新 1(Java):

在 Selenium 中有 4 种点击方式。

我将使用这个 xpath

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

代码试用 1:

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

代码试用 2:

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

代码试用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);

代码试用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();

更新 2:

ArrayList<String> tabs =  new ArrayList<String>(driver.getWindowHandles());
driver.switchTo().window(tabs.get(1));

推荐阅读