我沒有遇到任何錯誤,因為沒有找到任何元素,但是我的測驗用例是在控制臺中傳遞的,但是當我檢查下載檔案夾時,它顯示了一些臨時檔案而不是實際的影像檔案。如果有人解決了這個問題,這將非常有用。
driver.get("https://demoqa.com/elements");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// Here normal 'findElement' is not working, hence used the javascript executor
WebElement leftmenu = driver.findElement(By.xpath("(//li[@id='item-7']//span)[1]"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", leftmenu); //clicking the left menu
Thread.sleep(5000);
driver.findElement(By.xpath("//a[@download='sampleFile.jpeg']")).click(); // download button
uj5u.com熱心網友回復:
單擊下載按鈕后,您正在完成測驗運行并立即關閉瀏覽器。
單擊它后嘗試添加一個簡單的睡眠。
此外,您不應該使用這樣的硬編碼暫停
Thread.sleep(5000);
應改為使用顯式等待。
也請嘗試等到元素可見,就像我在這里寫的那樣。我認為這會讓您使用常規驅動程式.click()方法單擊它。
試試這個:
driver.get("https://demoqa.com/elements");
driver.manage().window().maximize();
WebDriverWait wait = new WebDriverWait(driver, globalDelay);
// Here normal 'findElement' is not working, hence used the javascript executor
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("(//li[@id='item-7']//span)[1]"))).click();
//WebElement leftmenu = driver.findElement(By.xpath("(//li[@id='item-7']//span)[1]"));
//JavascriptExecutor executor = (JavascriptExecutor)driver;
//executor.executeScript("arguments[0].click();", leftmenu); //clicking the left menu
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[@download='sampleFile.jpeg']"))).click();
Thread.sleep(5000);
uj5u.com熱心網友回復:
要在元素上單擊(),Download您可以使用以下任一定位器策略:
鏈接文本:
driver.findElement(By.linkText("Download")).click();css選擇器:
driver.findElement(By.cssSelector("a#downloadButton[download^='sampleFile']")).click();路徑:
driver.findElement(By.xpath("//a[@id='downloadButton' and starts-with(@download, 'sampleFile')]")).click();
理想情況下,要click()的元素,你需要引起WebDriverWait的elementToBeClickable(),你可以使用以下的定位策略:
鏈接文本:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.linkText("Download"))).click();css選擇器:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a#downloadButton[download^='sampleFile']"))).click();路徑:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@id='downloadButton' and starts-with(@download, 'sampleFile')]"))).click();
PS:點擊后Download不要立即關閉網路瀏覽器,等待下載程序完成。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/386777.html
