简体   繁体   English

如何使用jquery和selenium访问“ chrome:// downloads”处“ shadow-root”下的元素?

[英]How to access elements under `shadow-root` at 'chrome://downloads' using jquery and selenium?

I am trying to get the name of the last downloaded file in my selenium javascript application. 我正在尝试获取硒javascript应用程序中最后下载的文件的名称。

I have my selenium driver navigating to the chrome downloads page using: driver.get('chrome://downloads'); 我的硒驱动程序使用以下driver.get('chrome://downloads');导航至chrome下载页面: driver.get('chrome://downloads'); , but when I get there, selenium is not able to find ANY elements on the download page. ,但是当我到达那里时,硒无法在下载页面上找到任何元素。

The chrome downloads page 'chrome://downloads' has a bunch of shadow-root elements that I don't know how to get underneath in order to access the id's that I want. chrome下载页面“ chrome:// downloads”具有一堆shadow-root元素,我不知道该如何找到它们以访问所需的ID。 How do I access identifiers beneath shadow-root items? 如何访问shadow-root项目下的标识符?

I want to get $("#file-link") as shown here: 我想得到$(“#file-link”),如下所示:

显示文件链接ID

But when I use jquery to find it, everything returns null (probably because it's behind shadow-root ) 但是当我使用jquery来查找它时,一切都返回null(可能是因为它在shadow-root

jQuery操作返回null jQuery操作返回null 2

Here's a big picture of all the information I have including showing that "#file-link" totally exists: 这是我所拥有的所有信息的全景图,包括表明“#file-link”完全存在:

chrome下载页面的html结构

The code I am using to wait for the element to exist is the same that I use for all elements in my application, so I think this is already working: 我用来等待元素存在的代码与我在应用程序中所有元素使用的代码相同,因此我认为这已经可以工作了:

driver.wait(until.elementLocated(By.id('downloads-manager')), 120000).then(function(){
    console.log("#downloads-manager shows");
    driver.findElement(By.id('downloads-manager')).then(function(dwMan){
        //How do I "open" #shadow-root now? :(
    });
});

Here is my version information: 这是我的版本信息:

  • Chromium v54.0.2840.71 铬v54.0.2840.71
  • Node v6.5.0 节点v6.5.0
  • ChromeDriver v2.27.440175 ChromeDriver v2.27.440175
  • selenium-webdriver v3.4.0 selenium-webdriver v3.4.0

Similar Question 类似问题

Links 链接

The $ from your example is not a shorthand for JQuery . 您示例中的$不是JQuery的简写。 It's function overridden by the page to locate an element by id only: 该函数被页面覆盖以仅通过id定位元素:

function $(id){var el=document.getElementById(id);return el?assertInstanceof(el,HTMLElement):null}

To select through the shadow DOM, you need to use the '/deep/' combinator. 要通过阴影DOM进行选择,您需要使用'/ deep /'组合器。

So to get all the links in the download page: 因此,要获得下载页面中的所有链接:

document.querySelectorAll("downloads-manager /deep/ downloads-item /deep/ [id=file-link]")

And with Selenium: 和硒:

By.css("downloads-manager /deep/ downloads-item /deep/ [id=file-link]")

Why not check the downloads folder directly? 为什么不直接检查下载文件夹? I do this for downloading Excel files. 我这样做是为了下载Excel文件。 I first clear the downloads folder, click the button to download the file, wait ~5 sec (varies by file size, internet speed, etc.), and then looking in the folder for a "*.xlsx" file. 我首先清除downloads文件夹,单击按钮下载文件,等待约5秒钟(根据文件大小,互联网速度等而变化),然后在文件夹中查找“ * .xlsx”文件。 This also has the benefit of working with any browser. 这也具有使用任何浏览器的好处。

C# Examples: C#示例:

/// <summary>
/// Deletes the contents of the current user's "Downloads" folder
/// </summary>
public static void DeleteDownloads()
{
    // Get the default downloads folder for the current user
    string downloadFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + "\\Downloads";
    // Delete all existing files
    DirectoryInfo di = new DirectoryInfo(directoryPath);
    foreach (FileInfo file in di.GetFiles())
    {
        file.Delete();
    }
    foreach (DirectoryInfo dir in di.GetDirectories())
    {
        dir.Delete(true);
    }
}

/// <summary>
/// Looks for a file with the given extension (Example: "*.xlsx") in the current user's "Download" folder.
/// </summary>
/// <returns>Empty string if files are found</returns>
public static string LocateDownloadedFile(string fileExtension)
{
    // Get the default downloads folder for the current user
    string downloadFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + "\\Downloads";
    DirectoryInfo di = new DirectoryInfo(downloadFolderPath);
    FileInfo[] filesFound = di.GetFiles(fileExtension);
    if (filesFound.Length == 0)
    {
        return "No files present";
    }
    else
    {
        return "";
    }
}

And then in my Test I can Assert.IsEmpty(LocateDownloadedFile); 然后在我的测试中,我可以Assert.IsEmpty(LocateDownloadedFile); This way if the assert fails, the error message if printed. 这样,如果断言失败,则打印错误消息。

Expected: String.Empty. 预期:String.Empty。 Actual: No files present. 实际:无文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM