How to tell the code to choose between two variables and make them equal?

Tags: c#,css,selenium,xpath

Problem :

I'm working on a unit test, the test platform is a dynamic website and so CSS Selector and xpath change very often. So I thought to myself maybe I can use both CSS and xpath to retrieve the same text.

The main issue I face right now is how to tell the code to equal the two retrieved strings, like that it does not matter if the xpath fails the CSS Selector will still work and vice versa.

Here is a simple code

string text1 = _driver.FindElement(By.XPath("xpathtext")).Text;
string text2 = _driver.FindElement(By.CssSelector("csstext")).Text;
text1 = text2;
//then use text1 or text2 

is there any clean and neat way of doing it?

Solution :

IWebElement element1 = driver.FindElementIfExists(By.XPath("xpathtext"));
IWebElement element2 = driver.FindElementIfExists(By.CssSelector("csstext"));

string value = (element1 != null ? element1.Text : (element2 != null ? element2.Text : null));

It relies on the FindElementIfExists extension method. It's just a matter of creating this class in your project:

public static class WebDriverExtensions
    public static IWebElement FindElementIfExists(this IWebDriver driver, By by)
        IWebElement result = null;

            result = driver.FindElement(by);
        catch { }

        return result;

    CSS Howto..

    Content property in CSS - how to put content into several paragraphs

    How do I show annotations on my Google Chart that extracts data from a Google Spreadsheet?

    Responsive portrait images, Hows & Dont's?

    How can I make it so that my logo on the top left of the page lines up with the text underneath it?

    How to keep the footer docked at the bottom of the page considering following structure? [duplicate]

    How to make one list-item in menu higher than the others with CSS?

    How to make some controls visible and overlapping a DIV when mouse hovers on it? [closed]

    Global CSS - how to implement multiple frameworks

    How to make container 100% of parent if it have ul inside

    My jquery slideshow is not functioning properly

    simple css issue - how to change anchor inside li if li:hover

    How to find a div / panel in repeater and apply css?

    How get device resolution for loading sharp images but size fonts/divs properly with viewport width=device-width?

    How do I align my radio button with associated text?

    How to animate searchbox length from focusing on the textbox class CSS3?

    How do I do this CSS border-bottom

    How can I center using percentages?

    How to add image at right bottom div using css

    How to position the arrow of this widget correctly?

    How to do make CSS Toggle like this? [closed]

    How to write css rule that run with a special browser?

    How to get the first letter of each word to be a different size and the rest to be the same size

    How to make 1 image tile in css?

    How to see CSS boxes in firefox inspector

    How to change different language to different font-size at same line without unique each container?

    How can I shift up a div and all divs that follow it?

    How to create a floating mutlicolumn layout in CSS (Flexbox, float, inline-block)?

    How to add an image to xml sheet using css

    How to create a menu that rolls down? CSS

    How to display size and icon for pdf and word docs?