1

Hello guys can you please help me with my problem? I have method where i check if several web objects are preset on page and if so then i click on them, it is working as intended however i like use only one FindElements() for it, is it possible?Can someone tell me how? Thanks

My elements :

 public static class Elements
    {                    
        public static string allow = "//button[@data-testid='allow']";
        public static string addToHomescreen="//button[@datatestid='addToHomeScreen']";
    }

My method inside for loop :

                var allow = driver.FindElements(By.XPath(Elements.allow));
                var addToHS = driver.FindElements(By.XPath(Elements.addToHomescreen));
                var newLike = driver.FindElements(By.XPath(Elements.like));

                if(allow.Count == 0 && addToHS.Count == 0 newLike.Count > 0)
                {
                    driver.FindElement(By.XPath(Elements.like)).Click();
                }
                else if(allow.Count > 0)
                {
                    allow[0].Click();
                    Utils.Wait(1);
                    driver.FindElement(By.XPath(Elements.like)).Click();
                }

I want something like but cant get it done :

var allPopups= driver.FindElements(By.XPath(Elements.allow),By.XPath(Elements.addToHomescreen),);

I just need to FindElements() return more types of Elements but i cant figuer out syntax. THanks

Martin Ma
  • 125
  • 1
  • 13
  • Instead you can try this :`var allPopups= driver.FindElements(By.XPath(Elements.allow + " | "+ Elements.addToHomescreen));` concat your xpaths with or logic. – Eldar Sep 03 '21 at 09:23
  • 1
    @Eldar - thank you resolved, i was doing that but i was using +"||"+ instead :) – Martin Ma Sep 03 '21 at 09:28

1 Answers1

1

You can do this in xpath itself, there is or keyword in xpath.

public static string allow_addToHomescreen = "//button[@data-testid='allow' or @datatestid='addToHomeScreen']";

also the below is equivalent too:-

public static string allow_addToHomescreen = "//button[@data-testid='allow' | @datatestid='addToHomeScreen']";

See more details click here

cruisepandey
  • 28,520
  • 6
  • 20
  • 38