0

I'm written a test which i'm now trying to enhance and make it do more things.

I understand how to find a title of a webpage by using the following code

String title = webDriver.Title;

But if i now want to also output the header of the page how would i go about doing this? This is the web URL: https://energy.gocompare.com/gas-electricity#/ I've found the element: Switch today and save on your energy bills

Can i use an XPath to find this? then Console.WriteLine the output?

1 Answer 1

1

You can use this code :

String element = webDriver.FindElement(By.CssSelector("h1.c-header__heading")).Text;
Console.WriteLine("This is the text extracted from " +element);

XPATH :

//h1[@class='c-header__heading']

In code, you can use it like :

String someText= webDriver.FindElement(By.XPath("//h1[@class='c-header__heading']")).Text;
Console.WriteLine("This is the text extracted from " +someText);
Sign up to request clarification or add additional context in comments.

2 Comments

Can i create a loop so that it validates the output of the header. String header =webDriver.FindElement(By.CssSelector("h1.c-header__heading")).Text; if(header.Contains(header)) { Console.WriteLine("jriofjio"); } else { Console.WriteLine("incorrect"); }
No need to use loop , if you have only one text. Did that work for you ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.