Search code examples
c#visual-studioseleniumselenium-webdriverwebclient

How can I decide is there a specific text in web site withSelenium


EDITED POST

First of all, there is a similar question on site but it did not work that's why I am asking. I should decide is there a specific text LİSTELENECEK VERİ BULUNAMAMIŞTIR. on HTML code, there are only id attribute here is the HTML code:

<center id="genelUyariCenterTag">LİSTELENECEK VERİ BULUNAMAMIŞTIR.</center>

Here is the C# code:

 WebClient client = new WebClient();
void goMadde15Down() {
                    driver.FindElement(By.LinkText("İŞVEREN")).Click();
                    driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
                    driver.FindElement(By.LinkText("TEŞVİKLER VE TANIMLAR")).Click();
                    driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
                    driver.FindElement(By.LinkText("4447/GEÇİCİ 15. MADDE LİSTELEME/SİLME")).Click();
                    driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
                    string siteSource15 = client.DownloadString("https://uyg.sgk.gov.tr/IsverenSistemi");

                    string strText = driver.FindElement(By.XPath("//center[@id='genelUyariCenterTag' and contains(.,'BULUNAMAMIŞTIR')]")).Text;

                    if (strText.Contains("LİSTELENECEK VERİ BULUNAMAMIŞTIR."))
                    {
                        Console.WriteLine("madde15 there is no Excel");
                    }
                    else
                    {
                        Console.WriteLine("madde 15 there is an Excel");
                    }

The problem is, the condition always converge to else clause


Solution

  • Instead of if and else If you can use try-catch blocks you can use following code:

     void goMadde15Down() {
           driver.FindElement(By.LinkText("İŞVEREN")).Click();
           driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
           driver.FindElement(By.LinkText("TEŞVİKLER VE TANIMLAR")).Click();
           driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
           driver.FindElement(By.LinkText("4447/GEÇİCİ 15. MADDE LİSTELEME/SİLME")).Click();
           driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
    
                try  {
                        Console.WriteLine("There is an Excel File");
                        //Download code here
    
    
                     }
                catch (Exception e)
                     {
                            Console.WriteLine("There is no Excel File");
                     }
    

    Also try catch block is more efficient then if and else