0

I'm trying to manipulate "chrome://downloads/" URL using Selenium, my objective is to start a download, make sure that download get start and cancel the download. But I´m not able to get the values of the downloads or cancel.

It says that my id isn't correct, but I'm pretty sure that its correct. Does anyone know if there any restriction to this kind of page?

My code is --

using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text.RegularExpressions;
using System.Threading;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
namespace SitesApplications
{
    class Program
    {
        static void Main(string[] args)
        {

            BrowseUrl.driver = new ChromeDriver();

            //ChromeOptions TESTE = new ChromeOptions();
            //TESTE.AddArgument("--disable-privacy");





            FileDownload teste = new FileDownload("http://centos.ufes.br/7/isos/x86_64/CentOS-7-x86_64-Everything-1708.iso", 60);

            teste.gotoDownloadList("chrome://downloads/", 60);

            //ChromeWebElement haha = new ChromeWebElement(BrowseUrl.DriverChrome, "pause-or-resume");

            ////haha.Clicks(20);

            //haha.Cliquei(20);

            teste.CancelaTeste(10);

        }

    }

And the program Calls That

using OpenQA.Selenium;
using OpenQA.Selenium.Support.PageObjects;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SitesApplications
{
    class FileDownload
    {
        /// <summary>
        /// Enta no link direto para o download.
        /// </summary>
        /// <param name="Url">Link de destino para o download.</param>
        /// <param name="time">Tempo maximo para abertura da pagina.</param>
        public FileDownload(string Url, int time)
        {
            BrowseUrl CentOs = new BrowseUrl(Url, time);
            PageFactory.InitElements(BrowseUrl.driver, this);
        }

        public void gotoDownloadList(string url, int timeout)
        {

            BrowseUrl downList = new BrowseUrl(url, timeout);
            PageFactory.InitElements(BrowseUrl.driver, this);

        }

        private bool barra_prog;

        public bool barraprog
        {
            get { return barra_prog; }

        }


        // Cancela Download
        [FindsBy(How = How.TagName, Using = "Cancelar")]
        public IWebElement btnCancelD { get; set; }

        // Status do Download
        [FindsBy(How = How.Id, Using = "description")]
        public IWebElement status { get; set; }


        /// Cancela teste de download
        public bool CancelaTeste(int timeout)
        {

            BrowseUrl.driver.SwitchTo().Window("");

            //barra_prog = GetElements.GetCheckBoxAttribute(status, timeout);


            btnCancelD.Clicks(timeout);

            //BrowseUrl.driver.Close();
            //BrowseUrl.driver.Quit();

            return barra_prog;

        }
    }
Frank
  • 831
  • 1
  • 11
  • 23

1 Answers1

0

To know how to handle this Chrome download page you have to know how to handle Shadow DOM Elements. I tried a lot of different solutions before I finally managed to get the one below working:

 IJavaScriptExecutor jse2 = (IJavaScriptExecutor)Driver;
        jse2.ExecuteScript("return document.querySelector('downloads-manager').shadowRoot.querySelector('downloads-item').shadowRoot.getElementById('cancel').click()");

In your case you have to replace 'cancel' by 'Cancelar'. This answer is based on one of the answers to this Stackoverflow Question : Accessing Shadow DOM tree with Selenium

You can read an interesting article about Shadow DOM Elements here: http://www.seleniumeasy.com/selenium-tutorials/accessing-shadow-dom-elements-with-webdriver

Frank
  • 831
  • 1
  • 11
  • 23