コード例 #1
0
        public async Task <WebPoint> GetElementClickableLocation(string elementId, CancellationToken cancellationToken = new CancellationToken())
        {
            var targetElementId = elementId;
            var tagName         = await GetElementTagName(targetElementId, cancellationToken).ConfigureAwait(false);

            if (tagName == "area")
            {
                var func    = "function (element) {" + "  var map = element.parentElement;" + "  if (map.tagName.toLowerCase() != 'map')" + "    throw new Error('the area is not within a map');" + "  var mapName = map.getAttribute('name');" + "  if (mapName == null)" + "    throw new Error ('area\\'s parent map must have a name');" + "  mapName = '#' + mapName.toLowerCase();" + "  var images = document.getElementsByTagName('img');" + "  for (var i = 0; i < images.length; i++) {" + "    if (images[i].useMap.toLowerCase() == mapName)" + "      return images[i];" + "  }" + "  throw new Error('no img is found for the area');" + "}";
                var frameId = Session == null ? "" : Session.GetCurrentFrameId();
                var res     = await WebView.CallFunction(func, $"{{\"{Session?.GetElementKey()}\":\"{targetElementId}\"}}", frameId, true, false, cancellationToken).ConfigureAwait(false);

                targetElementId = ResultValueConverter.ToElementId(res?.Result?.Value, Session?.GetElementKey());
                //return ResultValueConverter.ToWebPoint(res?.Result?.Value);
            }

            var isDisplayed = await IsElementDisplayed(targetElementId, cancellationToken).ConfigureAwait(false);

            if (isDisplayed)
            {
                var rect = await GetElementRegion(targetElementId, cancellationToken).ConfigureAwait(false);

                var location = await ScrollElementRegionIntoView(targetElementId, rect, true, elementId, cancellationToken).ConfigureAwait(false);

                //if (location == null) return null;
                //var res = location.Offset(rect.Width / 2, rect.Height / 2);
                return(location.Offset(rect.Width / 2, rect.Height / 2));
            }

            return(null);
        }
コード例 #2
0
        public async Task <string> GetActiveElement(CancellationToken cancellationToken = new CancellationToken())
        {
            var func    = "function() { return document.activeElement || document.body }";
            var frameId = Session == null ? "" : Session.GetCurrentFrameId();
            var res     = await webView.CallFunction(func, null, frameId, true, false, cancellationToken);

            return(ResultValueConverter.ToElementId(res?.Result?.Value, GetElementKey()));
            //return res?.Result?.Value as JToken;
        }