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);
  • @FreddieMoore. So you are saving the page source into a string? If you are navigating to the page using a WebDriver, I would not recommend using the pagesource because you'll have to use string manipulation to get the info you need.
    – Raoul Duke
    Jul 5 '19 at 18:07
  • @FreddieMoore, if you can, please edit your post to include some example code
    – Raoul Duke
    Jul 5 '19 at 18:07

C# has a Object.ToString method that can serve to this purpose.

So, you can just create an object that represent this info, with attribute such as name and address and override Object.ToString, returning a String with the content you are interested.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.