//test6からソース取得を削ってtest7のテキスト取得だけにした物 クリップボードからテキストにするバッチファイルも出す仕様にした。
using System;
using System.IO;
using System.Threading;
using System.Diagnostics;// 外部ファイル実行に必要
using System.Text;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Interactions;
using OpenQA.Selenium.Support.UI;// WebDriverWaitを使うには必要
IWebDriver driver = new ChromeDriver();
// コードページ エンコーディング プロバイダーを登録
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);// Shift-JIS や EUC-JP使う場合に必要
//自分自身の実行ファイルのパスを取得し、作業ディレクトリを移動する
string appPath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
Directory.SetCurrentDirectory(appPath);
Console.WriteLine(appPath);
//URLの書いてあるread.txtを読む
StreamReader sr = new StreamReader("read.txt", Encoding.UTF8);
// StreamReader sr = new StreamReader("read.txt", Encoding.GetEncoding("Shift_JIS"));
string str = sr.ReadToEnd();
sr.Close();
string huga = "";
System.Console.WriteLine(huga);System.Console.WriteLine(huga);System.Console.WriteLine(huga);
// Cromeで、読んだURLを開く
driver.Navigate().GoToUrl(str);
//driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait.Until(d => d.FindElement(By.TagName("body"))); // bodyタグが読み込まれるのを待機
Thread.Sleep(1000);//3秒待つ
// Seleniumのkeyでキー操作
driver.FindElement(By.TagName("html")).SendKeys(OpenQA.Selenium.Keys.LeftControl + "a" );Thread.Sleep(500);
driver.FindElement(By.TagName("html")).SendKeys(OpenQA.Selenium.Keys.LeftControl + "c" );
Thread.Sleep(2000); // 2秒待機
// バッチファイルでクリップボードの内容をテキストにする
//string outc = "powershell -Command \"Get-ClipBoard\" > out2.txt";//バッチを作る
string outc = "powershell -Command \"Get-ClipBoard | Out-File -FilePath out2.txt -Encoding utf8\""; //バッチを作る
File.WriteAllText("txtput.bat", outc);
Thread.Sleep(500);
var startInfo = new ProcessStartInfo("txtput.bat")
{WindowStyle = ProcessWindowStyle.Hidden}; // 非表示で実行
Process.Start(startInfo)?.WaitForExit();
driver.Quit();