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;

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

        return result;
    }    
}

    CSS Howto..

    SCSS has the color in rgba() format, but the color is in hex format. How to keep the color in RGBA() format in the genrated css?

    Wordpress Plugin - How can I make the text responsive, OR replace it with a new div?

    How to move one element to the right of another using CSS?

    How can I make my Div box holder look like this inside with CSS?

    CSS Hover Events, How to Trigger a Parent's Effect

    How to select a single child class based on its parent class on CSS and jquery?

    Can't figure out how to make a box with multiple images with CSS/HTML

    How can I use .htaccess to send distant expiry headers on CSS and JS files?

    How to get and set CSS class in TinyMCE editor?

    how to achieve facebook mobile site buttons menu scroll effect?

    How to resize content area with CSS dynamically and no javascript

    CSS How to apply child:hover but not parent:hover

    How to add border to circular image on hover? css or jquery

    CSS/HTML problem in IE - how to fix?

    How to use a full width image as border between content and footer

    how to add a custom font to rails app

    HTML CSS How to stop a table to put the content after it on a new line?

    How to get the effect not to add one more div?

    How to freeze frame with css

    How to make my first-child in css work properly?

    How can i make it through using bootstrap grid?

    Using CSS, how to create a two-column grid with equal-width columns that are “only as narrow as required”?

    how to code a stickyfooter in css?

    How to zoom in to a specific point smoothly with CSS?

    How can I add fixed background just to this section? [demo included]

    How to move an image upwards in html/css

    How does Slick js carousel behave width custom css?

    PNG image showing up with white background even though it has a clear background

    How to line up the bottom of side-by-side Div layers in CSS

    How can I import a single string line of text to multiple HTML files?