2015-09-17 9 views
6

これはうまく動作していたようですが、もう機能していないようです。おそらくそれを可能にするいくつかのトグルがありますか?このコードを使用してUIオートメーションを使用してエッジブラウザからテキストを取得する方法

private static async Task<string> getText(double x, double y) 
{ 
    try 
    { 
     var location = new System.Windows.Point(x, y); 
     AutomationElement element = AutomationElement.FromPoint(location); 

     object patternObj; 
     if (element.TryGetCurrentPattern(TextPattern.Pattern, out patternObj)) 
     { 
      var textPattern = (TextPattern)patternObj; 

      var range = textPattern.RangeFromPoint(location); 
      range.ExpandToEnclosingUnit(TextUnit.Word); 

      var text = range.GetText(-1).Trim(); 
      return text; 
     } 
     else 
     { 
      return "no text found"; 
     } 
    } 
    catch (Exception ex) 
    { 
     return ex.Message; 
    } 
} 

それはたブラウザとメトロのアプリで作業を行います(あなたが速すぎてスクロールする場合のビットフレーク状が)。マニフェストについては、私はuiAccess = true、AsInvokerを使用しています。管理者として実行されても、それは役に立ちません。 同じことができるのであれば、WebDriverを使用するソリューションも許容されます。マイクロソフトのエッジを書いている時点で

+0

どの時点では、コードが機能しなくなっていますか?たとえば、Textパターンはサポートされていないか、RangeFromPoint()は予期しない範囲を返します。)TextパターンをサポートするEdgeに要素が存在する必要があります。だから、おそらくFromPoint()はその要素を返さない。あなたはその要素のプロパティをチェックして、あなたが持っている要素を判断できますか? Textパターンをサポートする要素でない場合は、FromPoint()によって返される要素からTextパターン要素に移動できます。 –

+0

私はそれが必要なのですが、ポイントが機能しない可能性があります。 – tofutim

+4

あなたの質問は一般的なケースではうまくいきすぎるほど広すぎます。完全な復習ケースを提供してください。 –

答えて

0

はCodedUIでサポートされていない、彼らは支援を評価しているが、現在、あなたがそれを使用することはできません言及している:このリンクは問題に出願されたバグを示していますhttps://connect.microsoft.com/VisualStudio/feedback/details/1551238/vs2015-supports-codedui-automation-test-for-edge-browser

webdriverをです現在はMicrosoft Edgeの自動化を行う最善の方法です。しかし、上記のコードを見ても、まったく同じことをすることはできません。 WebDriverでは、Id、ClassName、TagName、Name、LinkText、部分リンクテキスト、CSS、Xpathで要素を見つけることができますが、私が知っている限り、x、y座標からオブジェクトを見つけることはできません上記の例。

Webdriverを使い始めるにはコンソールアプリケーションを作成します。以下のパッケージをインストールします。

Install-Package Selenium.RC 
Install-Package Selenium.WebDriver 
Install-Package Selenium.WebDriverBackedSelenium 
Install-Package Selenium.Support 

は、お使いのオペレーティングシステムに応じて、正しいマイクロソフトwebdriverををインストールします。Windows 10の場合

Microsoft WebDriverの詳細については、hereを参照してください。あなたは、その後、webdriverをを駆動するためにいくつかのコードを追加することができ、次の例では、私のブログに行くと、それのCSSクラス名を経由して要素を取得し

:予想通り

using System; 
using System.IO; 
using OpenQA.Selenium; 
using OpenQA.Selenium.Edge; 
using OpenQA.Selenium.Remote; 
using OpenQA.Selenium.Support.UI; 

namespace SampleGetText 
{ 
    public class Program 
    { 
     static void Main(string[] args) 
     { 
      var text = GetText(); 
     } 

     public static string GetText() 
     { 
      RemoteWebDriver driver = null; 
      string serverPath = "Microsoft Web Driver"; 
      // Makes sure we uses the correct ProgramFiles depending on Enviroment 
      string programfiles = Environment.Is64BitOperatingSystem ? "%ProgramFiles(x86)%" : "%ProgramFiles%"; 

      try 
      { 
       // Gets loaction for MicrosoftWebDriver.exe 
       serverPath = Path.Combine(System.Environment.ExpandEnvironmentVariables(programfiles), serverPath); 

       //Create a new EdgeDriver using the serverPath 
       EdgeOptions options = new EdgeOptions(); 
       options.PageLoadStrategy = EdgePageLoadStrategy.Eager; 
       driver = new EdgeDriver(serverPath, options); 

       //Set a page load timeout for 5 seconds 
       driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(5)); 

       // Navigate to my blog 
       driver.Url = "https://blogs.msdn.microsoft.com/thebeebs/"; 

       // Find the first element on my screen with CSS class entry-title and return the text 
       IWebElement myBlogPost = driver.FindElement(By.ClassName("entry-title")); 
       return myBlogPost.Text; 
      } 
      catch (Exception e) 
      { 
       Console.WriteLine(e.Message); 
       return ""; 
      } 
      finally 
      { 
       if (driver != null) 
       { 
        driver.Close(); 
       } 
      } 
     } 
    } 
} 
関連する問題