hasan hüseyin
hasan hüseyin

Reputation: 33

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

Upvotes: 0

Views: 102

Answers (2)

Onur Ege Unaldi
Onur Ege Unaldi

Reputation: 185

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

Upvotes: 1

KunduK
KunduK

Reputation: 33384

Induce WebDriverWait and ElementExists and use below xpath.

You need to install SeleniumExtras.WaitHelpers pakage from Manage Nuget Packages.

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
string strText=wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementExists(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");
             }

Try updated one without WebDriverWait

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");
                }

Upvotes: 0

Related Questions