0

I am new to automation, I just learned about Selenium for 3 days and built a simple C# console application with Selenium which trying to automate "sell" request on a trading website.

I noticed when I login the trading website with normal Chrome, the site is working as normal. But when I launch it with my C# console app, it seems like the trading website is not functioning as normal, seems dead.

Here I attached with both developer console picture as reference.

Normal Chrome browser: enter image description here

Launched with C# console app: enter image description here

Am I missing any part of my code, and cause the launched Chrome is being "locked".

Please help me

below is my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
using WebDriverManager;
using WebDriverManager.DriverConfigs.Impl;


namespace ii.tfxBot
{
    class Program
    {

        static void Main(string[] args)
        {
            string ExecuteUrl = "https://tex.tfxi.com/#/exchange/tfx_usdt";

            Console.WriteLine("test case started ");
            //create the reference for the browser  
            //new DriverManager().SetUpDriver(new ChromeConfig());

            var ccOptions = new ChromeOptions();
            ccOptions.LeaveBrowserRunning = true;

            //ccOptions.AttachToEdgeChrome = true;
            //change the path accordingly
            //ccOptions.EdgeExecutablePath = "C:/Program Files (x86)/Microsoft/Edge/Application/msedge.exe";
            new DriverManager().SetUpDriver(new ChromeConfig());

            var driver = new ChromeDriver(@"C:\WebDriver\bin", ccOptions);

            // navigate to URL  
            driver.Navigate().GoToUrl("https://tex.tfxi.com/#/login");
            Thread.Sleep(2000);
            // identify the Google search text box  
            //IWebElement ele = driver.FindElement(By.Name("q"));
            ////enter the value in the google search text box  
            //ele.SendKeys("javatpoint tutorials");
            //Thread.Sleep(2000);
            ////identify the google search button  
            //IWebElement ele1 = driver.FindElement(By.Name("btnK"));
            //// click on the Google search button  
            //ele1.Click();
            bool isUrl = false;
            while (isUrl == false)
            {
                if (driver.Url == ExecuteUrl)
                {
                    StartExecution(driver);
                    break;
                }
                Console.WriteLine("Execute Url not matched... try again in 1 second");
                Thread.Sleep(5000);
            }


            //close the browser  
            //driver.Close();
            Thread.Sleep(8000000);
            Console.WriteLine("test case ended ");

        }

        private static void StartExecution(ChromeDriver driver)
        {

            var inputElements = driver.FindElements(By.CssSelector(".ivu-input.ivu-input-default"));
            int i = 0;

            foreach (var inputElement in inputElements)
            {
                Console.WriteLine("input count " + i.ToString() + " : " + inputElement.GetAttribute("placeholder").ToString());

                string plText = inputElement.GetAttribute("placeholder").ToString();

                if (plText == "出售數量")
                {
                    Console.WriteLine("found the input number: " + i.ToString());

                }
                else
                {
                    //inputElement.SendKeys("28000");
                }
                i++;
            }



            WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromMilliseconds(300));
            IWebElement divLimit;
            bool found = false;
            while (found == false)
            {
                //divLimit = driver.FindElement(By.ClassName("bd bd_limited"));
                //divLimit = wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.CssSelector("bd bd_limited")));
                divLimit = wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementExists(By.CssSelector(".bd.bd_limited"))); ;

                if (divLimit != null)
                {
                    Console.WriteLine("item found, proceed to get selling price!");
                    GetSellingPrice(divLimit);
                    break;
                }

                Console.WriteLine("item NOT found!");
                Thread.Sleep(300);

            }

            int tfxBal = GetTfxBalance(driver);
            WaitforElement(driver, By.CssSelector(".mask2"), tfxBal);

        }

        public static int GetTfxBalance(ChromeDriver driver)
        {
            int TfxBal = 0;
            WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromMilliseconds(300));

            IWebElement divHdLogin;
            divHdLogin = wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementExists(By.CssSelector(".hd.hd_login")));

            //(IWebElement)driver.FindElements(By.CssSelector(".hd.hd_login"));

            var targetKeys = divHdLogin.FindElements(By.TagName("span"));

            Console.WriteLine("targetKeys 0 : " + targetKeys[0].Text);
            Console.WriteLine("targetKeys 1 : " + targetKeys[1].Text);
            Console.WriteLine("targetKeys 2 : " + targetKeys[2].Text);
            decimal o = Convert.ToDecimal(targetKeys[1].Text);

            if (targetKeys[1].Text.Contains("."))
            {
                var a = targetKeys[1].Text.Split('.');
                TfxBal = Convert.ToInt32(a[0]);
            }
            else
            {
                TfxBal = Convert.ToInt32(targetKeys[1].Text);
            }
            return TfxBal;
        }

        public static void GetSellingPrice(IWebElement divLimit)
        {
            IWebElement divFormContent = divLimit.FindElement(By.CssSelector(".ivu-form-item-content"));
            IWebElement ipSellPrice = divFormContent.FindElement(By.CssSelector(".ivu-input.ivu-input-default"));
            Console.WriteLine(ipSellPrice.GetAttribute("value"));
        }

        public static bool Elexists(By by, WebDriver driver)
        {
            try
            {
                driver.FindElement(by);
                IWebElement divPopup = driver.FindElement(by);
                WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromMilliseconds(300));

                // textLeft countNumber
                IWebElement divcountNumber;
                divcountNumber = wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementExists(By.CssSelector(".textLeft.countNumber")));

                //Console.WriteLine("count number: " + divcountNumber.Text);
                string popupStyle = divPopup.GetAttribute("style");
                //Console.WriteLine("div Popup Style : " + popupStyle);
                if (popupStyle == "display: none;")
                {
                    //Console.WriteLine("No Pop up..");
                    return false;
                }
                else
                {
                    Console.WriteLine("Pop up display has changed!");
                    return true;
                }
            }
            catch (NoSuchElementException)
            {
                return false;
            }
        }

        public static void WaitforElement(WebDriver driver, By by, int tfxBalance = 0)
        {
            // waitforMin = 2 hours
            int waitforMin = 60 * 1000 * 2 * 2 * 2;

            for (int i = 0; i < waitforMin; i++)
            {
                System.Threading.Thread.Sleep(250);
                if (Elexists(by, driver))
                {
                    Console.WriteLine("Pop up appears! Performing selling now");

                    var inputElements = driver.FindElements(By.CssSelector(".ivu-input.ivu-input-default"));

                    IWebElement SellBtn = driver.FindElement(By.CssSelector(".bg-red.ivu-btn.ivu-btn-default.ivu-btn-long"));
                    foreach (var inputElement in inputElements)
                    {

                        string plText = inputElement.GetAttribute("placeholder").ToString();

                        if (plText == "出售數量")
                        {
                            Console.WriteLine("found the input box");
                            inputElement.SendKeys("28000");
                            SellBtn.Click();
                        }
                    }
                    break;
                }
                Console.Write(".");
            }
        }

        public static void SellTfx()
        {
            //
        }
    }
}

I am expecting some solution to fix my code

0 Answers0