我一直在嘗試使用Java的Selenium WebDriver。我可以很容易地完成對帶有 ID 的按鈕的點擊,通過值從下拉框中選擇,甚至在輸入框中輸入。
我試圖制作一個自動化程式來瀏覽報告平臺上的不同類別,并通過填寫日期_start和日期_end來下載某些報告。進展順利,使用:
WebElement startDate = driver.findElement(By.id("start_date") )。)
startDate.sendKeys("25082021")。
WebElement endDate = driver.findElement(By.id("end_date") )。)
endDate.sendKeys("25092021")。
問題是最后要點擊一個下載按鈕'ExcelHtml5',這是Datatables的一部分。
我已經嘗試使用By.xpath()和By.cssSelector()來完成點擊
。但運氣不好。以下是我所嘗試的最新的幾行代碼:
WebDriverWait wait = new WebDriverWait(driver, 10)。
WebElement edit_button = wait.until(ExpectedConditions.elementToBeClickable(By. cssSelector("button#dt-button.button-excel.button-html5.btn.btn-success.btn-sm.downloadExcel""/span>))。
edit_button.click()。
還有一種方法:
WebElement excelButton = driver.findElement(By.xpath("//button[@class='downloadExcel'][@type='button'][contains(text(), 'Excel')]")。
excelButton.click()。
在不到半小時的時間里,我已經準備好了一切,除了點擊 "Excel "下載按鈕。我努力在網上尋找答案,最后用了大約6個小時,用Selenium WebDriver讓這個Excel按鈕被點擊,但沒有成功。我已經嘗試了chrome的擴展程式,如XPath Helper和Chropath來獲取cssSelector。
有誰能幫助我嗎?
Update: 問題是由程序中打開的一個新標簽引起的。在使用@cruisepandey的代碼解決方案后,我成功地下載了報告!
更正后的代碼為:
我的報告是由一個新的標簽打開的。
更正后的代碼是:
ArrayList<String> tabs = new ArrayList<String>(driver. getWindowHandles())。
driver.switchTo().window(tabs.get(1);
driver.findElement(By.xpath("//div[@class='dt-buttons']//button[包含(@class,'downloadExcel')]").click()。
uj5u.com熱心網友回復:
在Selenium中,有4種方法可以點擊。
我將使用這個xpath
。//div[@class='dt-buttons']//button[contains(@class, 'downloadExcel')]
代碼試用1 :
time.sleep(5)。
驅動。 find_element_by_xpath("//div[@class='dt-buttons']//button[contains(@class, 'downloadExcel')]")。 點擊()
代碼試用2 :
WebDriverWait(driver, 20). 直到(EC.element_to_be_clickable((By. XPATH, "//div[@class='dt-buttons']//button[包含(@class, 'downloadExcel')]"))。 點擊()
代碼試用3 :
time.sleep(5)
button = driver.find_element_by_xpath("//div[@class='dt-buttons']//button[包含(@class,'downloadExcel')]")
driver.execute_script("arguments[0].click();", button)
代碼試驗4 :
time.sleep(5)
button = driver.find_element_by_xpath("//div[@class='dt-buttons']//button[包含(@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中是否有唯一的條目
檢查的步驟:
在Chrome中按F12 -> 進入元素部分 -> 做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')]"/span>)。
((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().former()。
Update 2 :
ArrayList<String> tabs = new ArrayList<String>(driver. getWindowHandles())。
driver.switchTo().window(tabs.get(1) 。)
uj5u.com熱心網友回復:
試一下:
WebDriverWait wait = new WebDriverWait(driver, 10)。)
WebElement edit_button = wait. until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//button[contains(@class,'downloadExcel')]"/span>))。
edit_button.click()。
如果/button[contains(@class,'downloadExcel')] XPath不是唯一的定位器,可以這樣嘗試:
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()。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/309746.html
標籤:
