Is it possible to read / write a web page loaded i

2019-07-30 04:24发布

I realize this question isn't exactly up to SO's standards but I'm hoping someone can tell me I'm wasting my time or point me in the correct direction.

I'm wondering if there was a low level API for manipulating a web page loaded in Edge. I'm working on an automation project that needs to be able to parse the DOM, inject elements, get / set input values from a separate process in real time.

I have not been able to find ANYTHING online that suggests this is possible but I'm worried I might not even be looking in the right places.

Thanks!

1条回答
做自己的国王
2楼-- · 2019-07-30 05:24

Shawn, take a look at the Selenium WebDriver for Microsoft Edge. It's normally used as a QA tool, but I see no reason why you couldn't do those things this way.

From the W3C definition of the WebDriver

WebDriver is a remote control interface that enables introspection and control of user agents. It provides a platform- and language-neutral wire protocol as a way for out-of-process programs to remotely instruct the behavior of web browsers. Provided is a set of interfaces to discover and manipulate DOM elements in web documents and to control the behavior of a user agent. It is primarily intended to allow web authors to write tests that automate a user agent from a separate controlling process, but may also be used in such a way as to allow in-browser scripts to control a — possibly separate — browser

I believe that represents most of what you want to do. Here are some useful links that will help you get started.

Here is some sample C# code nabbed from a MSFT Gist

using OpenQA.Selenium;
using OpenQA.Selenium.Edge;
using System;

namespace EdgeDriverTests
{
    public class Program
    {
        /*
        * This assumes you have added MicrosoftWebDriver.exe to your System Path.
        * For help on adding an exe to your System Path, please see:
        * https://msdn.microsoft.com/en-us/library/office/ee537574(v=office.14).aspx
        */
        static void Main(string[] args)
        {
            /* You can find the latest version of Microsoft WebDriver here:
            * https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/
            */
            var driver = new EdgeDriver();

            // Navigate to Bing
            driver.Url = "https://www.bing.com/";

            // Find the search box and query for webdriver
            var element = driver.FindElementById("sb_form_q");

            element.SendKeys("webdriver");
            element.SendKeys(Keys.Enter);

            Console.ReadLine();
            driver.Quit();
        }
    }
}
查看更多
登录 后发表回答