0

Basically, I'm making a programme that logs into the krispy kreme site and to test if login was successful, I am searching for a certain web element. I'm using selenium.

For debugging purposes, and also as I may need to use this in future aspects of the program, I am trying to save the Element into a variable and then re-output with Console.WriteLine(), which works fine. When I try to use it in other scenarios e.g. If Statements. I get : cannot convert selenium element to string.

How do I convert to a string? and store it in a new string variable.

asked Jul 2, 2019 at 15:40

2 Answers 2

0

If you need to save the Element into a variable, you can do the following:

IWebElement submitbutton = driver.FindElement(By.Id("Submit"));

Beware, if you navigate to a different page and come back to the Krispy Kreme login page, the reference to that element will become "stale," so you will need call the line above again.

Whenever you need to preform an action (like click/submit) on that button you can use:

submitButton.Click();

or

submitButton.Submit();

If you would like output info in the form of a string, you could do the following:

string buttonText = submitButton.Text; //output the text on the button
string elementTypeAttribute = submitButton.getAttribute("type"); //outputs an html attribute of the element
string elementTagName = submitButton.TagName; 

I can't tell why your code is not working in the if statement from the information you've given. If you provide a code snippet then I can help. It sounds like you are trying to use the element as part of a conditional statement. If that is true, try using the methods above or using submitButton.ToString()

answered Jul 2, 2019 at 18:26
0

It's because every object in C# has ToString method. But you are do it wrong.

If you need string, please use .Text property on object (in your case WebElement). Always.

And please post full code if you have questions about it.

answered Jul 4, 2019 at 15:47

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.