我想從網頁 div 中復制 alter table 資料并將其粘貼到 .txt 檔案中,截圖如下:

以下是上述螢屏截圖的 HTML:

我可以通過將其存盤在像下面這樣的變數中來做到這一點,但是我怎樣才能一次將所有資料從 div 復制到變數中?
string value = driver.FindElement(By.XPath("//td[@style='padding:0px;
white-space: nowrap;']")).Text;
下面是我的測驗用例的代碼,其中我在轉換后從工具中選擇要轉換的檔案我想將更改表腳本存盤在單獨的 .txt 檔案中,為此我創建了一個創建函式來創建檔案:
public void TestMethod1()
{
try
{
string dir = @"D:\test\input"; //path
var directory = new DirectoryInfo(dir); //folder ko access
foreach (FileInfo file in directory.GetFiles()) //loop
IWebDriver driver = new ChromeDriver(); //driver object
driver.Navigate().GoToUrl("http:abcurl//convPLSQL.html");
//site url
driver.Manage().Window.Maximize(); //browser maximize
string param = dir.Replace("/", "\\"); // ye code file
param = "\\";
param = file.Name;
driver.FindElement(By.Id("fileuploader")).SendKeys(param);
driver.FindElement(By.Id("keyinput")).SendKeys("convUser001");//Key
driver.FindElement(By.Id("translatebutton")).Click();//Translate Button
driver.FindElement(By.LinkText("Download Results")).Click();//Download
// string data= driver.FindElement(By.XPath("//td[@style='padding:0px;
// white-space:nowrap;']")).Text;
create(); // call create function to create .txt file
}
public void create()
{
try
{
string fileName = @"D:\test\output\Mahesh.txt";
// Check if file already exists. If yes, delete it.
if (System.IO.File.Exists(fileName))
{
System.IO.File.Delete(fileName);
}
// Create a new file
using (FileStream fs = System.IO.File.Create(fileName))
{
// Add some text to file
Byte[] title = new UTF8Encoding(true).GetBytes("New Text File");
fs.Write(title, 0, title.Length);
byte[] author = new UTF8Encoding(true).GetBytes("Mahesh Chand");
fs.Write(author, 0, author.Length);
}
// Open the stream and read it back.
using (StreamReader sr = System.IO.File.OpenText(fileName))
{
string s = "";
while ((s = sr.ReadLine()) != null)
{
System.Console.WriteLine(s);
}
}
uj5u.com熱心網友回復:
獲取 div 的所有子元素,其中包含具有所需文本的 span。就像是:
var spans = driver.FindElements(By.XPath("//td/div[2]/span"));
然后連接來自每個 span 元素的文本。將“ ”等特殊字符替換為空格。使用字串生成器或將文本添加到字串通用集合,如果文本很大,請稍后加入。
例子:
var text = string.Empty;
foreach(var span in spans)
{
text = span.Text.Replace(" ", " ");
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/490581.html
上一篇:動態呼叫者給出錯誤MissingMethodException:Method'Clock.TEST'notfound
