Output Contents of an Element?



  • I am currently creating a tool. That will login into my Krispy Kreme account and then output my saved address. I am currently at the stage where I have managed to use the tool and : log into the account , navigate to the page where my address is saved.

    When inspecting the Krispy Kreme page (where my address is) this is the relevant information :

    address class="block-item_addresses"
    div data-id="name">John Smith<
    div data-id="lineOne">Flat 7 Big house<
    

    I'd ideally like to have all that data saved into one variable : "myaddress" and then output it with Console.writeline(myaddress);

    Many thanks!



  • If you want to save "John Smith \n Flat 7 Big House", you could use this:

    string addrss = driver.FindElement(By.ClassName("block-item_addresses")).Text;
    

    from there, you could create a new Address object that contains string name and string address properties, then save the element string into that object:

    public class Address
    {
        public Address(string name, string address)
        {
             this.name = name;
             this.address = address;
        }
    
        public string name { get; set; }
    
        public string address { get; set; }
    
    }
    

    To create a new Address object and output its contents:

    //get element text
    string addrss = driver.FindElement(By.ClassName("block-item_addresses")).Text;
    
    //separate the element text by new line
    var split = addrss.Split(new string[] {"\\n"}, StringSplitOptions.None);
    
    //create new Address object
    Address address = new Address(split[0], split[1]);
    
    //print Address props
    Console.WriteLine("Name: " + address.name);
    Console.WriteLine("Address: " + address.address);
    


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2