Home > database >  How to copy data from HTML div in Selenium C#?
How to copy data from HTML div in Selenium C#?

Time:06-14

I want to copy alter table data from webpage div and paste it into a .txtfile, screenshot is attached below:

enter image description here

Below is the HTML for above screenshot:

enter image description here

Can i do this by storing this in a variable like below but how can i copy all data at once in a variable from div ?

 string value = driver.FindElement(By.XPath("//td[@style='padding:0px; 
 white-space: nowrap;']")).Text;

Below is the code of my test case in which i am selecting a file to convert from a tool after conversion i want to store the alter table script in a separate .txt file for which i created a create function to create file :

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);
                }
            }

CodePudding user response:

Get all child elements of div containing the spans having the needed text. Something like:

var spans = driver.FindElements(By.XPath("//td/div[2]/span"));

Then concatenate text from each span element. Replace special characters like "&nbsp" with space. Use string builder or add text to string generic collection and join later if the text is big.

Example:

var text = string.Empty;
foreach(var span in spans)
{
    text  = span.Text.Replace("&nbsp", " ");
}
  • Related