Advertisement
Guest User

Untitled

a guest
Mar 1st, 2018
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 27.74 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace NekretnineScraperTest
  8. {
  9.     public class Oglas
  10.     {
  11.         public string Sifra { get; set; }
  12.         public string Vrsta { get; set; }
  13.         public string Naslov { get; set; }
  14.         public string Tip { get; set; }
  15.         public string Mesto { get; set; }
  16.         public string Lokacija { get; set; }
  17.         public string Opis { get; set; }
  18.         public string Ulica { get; set; }
  19.         public string Sobnost { get; set; }
  20.         public double Kvadratura { get; set; }
  21.         public double Cena { get; set; }
  22.         public double CenaPoKvadratu { get; set; }
  23.         public double Spratnost { get; set; }
  24.         public double Sprat { get; set; }
  25.         public string StanjeNekretnine { get; set; }
  26.         public int GodinaIzgradnje { get; set; }
  27.         public string Grejanje { get; set; }
  28.         public double BrojKupatila { get; set; }
  29.         public List<string> DodatneInformacije { get; set; }
  30.         public List<string> Pomagala { get; set; }
  31.  
  32.         public Oglas(string sifra, string naslov, string vrsta, string tip, string mesto, string lokacija, string ulica, string opis, string sobnost, double kvadratura, double cena, double cenapokvadratu, double ukupnoSpratova, double sprat, string stanjeNekretnine, int godinaIzgradnje, string grejanje, double brojKupatila, List<string> dodatneInformacije, List<string> pomagala)
  33.         {
  34.             this.Sifra = sifra;
  35.             this.Naslov = naslov;
  36.             this.Vrsta = vrsta;
  37.             this.Tip = tip;
  38.             this.Opis = opis;
  39.             this.Mesto = mesto;
  40.             this.Lokacija = lokacija;
  41.             this.Ulica = ulica;
  42.             this.Sobnost = sobnost;
  43.             this.Kvadratura = kvadratura;
  44.             this.Cena = cena;
  45.             this.CenaPoKvadratu = cenapokvadratu;
  46.             this.Spratnost = ukupnoSpratova;
  47.             this.Sprat = sprat;
  48.             this.StanjeNekretnine = stanjeNekretnine;
  49.             this.GodinaIzgradnje = godinaIzgradnje;
  50.             this.Grejanje = grejanje;
  51.             this.BrojKupatila = brojKupatila;
  52.             this.DodatneInformacije = dodatneInformacije;
  53.             this.Pomagala = pomagala;
  54.         }
  55.  
  56.         public Oglas()
  57.         {
  58.             this.DodatneInformacije = new List<string>();
  59.             this.Pomagala = new List<string>();
  60.         }
  61.  
  62.         public void AddProperties(List<string> Properties)
  63.         {
  64.             string[] splitter;
  65.             List<string> pomagala = new List<string>();
  66.             string key, value;
  67.             foreach (string property in Properties)
  68.             {
  69.                 splitter = property.Split(':');
  70.                 key = splitter[0].TrimStart().TrimEnd();
  71.                 value = "";
  72.                 try
  73.                 {
  74.                     if (splitter[1] != null)
  75.                     {
  76.                         value = splitter[1].TrimStart().TrimEnd();
  77.                     }
  78.                 }
  79.                 catch (Exception)
  80.                 {
  81.                    
  82.                 }
  83.                
  84.                
  85.  
  86.                 if (key.Contains("ล ifra"))
  87.                 {
  88.                     this.Sifra = value;
  89.                 }
  90.                 else if (key.Equals("Vrsta"))
  91.                 {
  92.                     this.Vrsta = value;
  93.                 }
  94.                 else if (key.Equals("Tip"))
  95.                 {
  96.                     this.Tip = value;
  97.                 }
  98.                 else if (key.Equals("Mesto"))
  99.                 {
  100.                     this.Mesto = value;
  101.                 }
  102.                 else if (key.Equals("Lokacija"))
  103.                 {
  104.                     this.Lokacija = value;
  105.                 }
  106.                 else if (key.Equals("Ulica"))
  107.                 {
  108.                     this.Ulica = value;
  109.                 }
  110.                 else if (key.Equals("Sobnost"))
  111.                 {
  112.                     this.Sobnost = value;
  113.                 }
  114.                 else if (key.Equals("Kvadratura"))
  115.                 {
  116.                     if (String.IsNullOrEmpty(value))
  117.                     {
  118.                         this.Kvadratura = -1;
  119.                     }
  120.                     else
  121.                     {
  122.                         this.Kvadratura = Math.Round(Double.Parse(value.Substring(0, value.Length - 2)), 2);
  123.                     }
  124.                    
  125.                 }
  126.                 else if (key.Equals("Cena"))
  127.                 {
  128.                     if (String.IsNullOrEmpty(value))
  129.                     {
  130.                         this.Cena = -1;
  131.                     }
  132.                     else
  133.                     {
  134.                         this.Cena = Math.Round(Double.Parse(value.Substring(0, value.Length - 1)), 2);
  135.                     }
  136.                    
  137.                 }
  138.                 else if (key.Equals("Cena po m2"))
  139.                 {
  140.                     if (String.IsNullOrEmpty(value))
  141.                     {
  142.                         this.CenaPoKvadratu = -1;
  143.                     }
  144.                     else
  145.                     {
  146.                         this.CenaPoKvadratu = Math.Round(Double.Parse(value.Substring(0, value.Length - 1)), 2);
  147.                     }
  148.                    
  149.                 }
  150.                 else if (key.Equals("Sprat"))
  151.                 {
  152.                     if (String.IsNullOrEmpty(value))
  153.                     {
  154.                         this.Spratnost = -1;
  155.                         this.Sprat = -1;
  156.                     }
  157.                     else
  158.                     {
  159.                         this.Spratnost  = Double.Parse(value.Trim().Split(new string[] { "od" }, StringSplitOptions.None)[1]);
  160.                         this.Sprat      = Double.Parse(value.Trim().Split(new string[] { "od" }, StringSplitOptions.None)[0]);
  161.                     }
  162.                    
  163.                 }
  164.                 else if (key.Equals("Stanje nekretnine"))
  165.                 {
  166.                     this.StanjeNekretnine = value;
  167.                 }
  168.                 else if (key.Contains("Godina"))
  169.                 {
  170.                     if (String.IsNullOrEmpty(value))
  171.                     {
  172.                         this.GodinaIzgradnje = -1;
  173.                     }
  174.                     else
  175.                     {
  176.                         this.GodinaIzgradnje = Int32.Parse(value);
  177.                     }
  178.                    
  179.                 }
  180.                 else if (key.Equals("Grejanje"))
  181.                 {
  182.                     this.Grejanje = value;
  183.                 }
  184.                 else if (key.Equals("Broj kupatila"))
  185.                 {
  186.                     if (String.IsNullOrEmpty(value))
  187.                     {
  188.                         this.BrojKupatila = -1;
  189.                     }
  190.                     else
  191.                     {
  192.                         this.BrojKupatila = Int32.Parse(value);
  193.                     }
  194.                    
  195.                 }
  196.  
  197.                 else if (key.Equals("Pomagala"))
  198.                 {
  199.                     if (!String.IsNullOrEmpty(value))
  200.                     {
  201.                         this.Pomagala.Add(value);
  202.                     }
  203.                    
  204.                 }
  205.                 else if (key.Contains("Rampa"))
  206.                 {
  207.                     this.Pomagala.Add(key);
  208.                 }
  209.                 else if (key.Equals("Lift"))
  210.                 {
  211.                     this.Pomagala.Add(key);
  212.                 }
  213.             }
  214.         }
  215.     }
  216. }
  217.  
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231. using System;
  232. using System.Collections.Generic;
  233. using System.Linq;
  234. using System.Text;
  235. using System.Threading.Tasks;
  236. using System.Windows.Forms;
  237.  
  238. namespace NekretnineScraperTest
  239. {
  240.     public class Scraper
  241.     {
  242.         //public TipOglasa TipOglasa { get; set; }  //Ovo ide u model
  243.         public WebBrowser Browser { get; set; }
  244.         public Scraper(WebBrowser browser)
  245.         {
  246.             this.Browser = browser;
  247.         }
  248.  
  249.         /// <summary>
  250.         /// Navigatuje se na oglas sa tom sifrom
  251.         /// </summary>
  252.         /// <param name="pageUrl"></param>
  253.         /// <param name="sifraOglasa"></param>
  254.         /// <returns></returns>
  255.         public void FindFullOglasUrl(string sifraOglasa)
  256.         {
  257.             Browser.Document.GetElementById("id_search").Children[0].SetAttribute("value", sifraOglasa);
  258.             Browser.Document.GetElementById("id_search").Children[1].InvokeMember("Click");
  259.         }
  260.  
  261.         /// <summary>
  262.         /// Vraca Oglas sa osnovnim podacima
  263.         /// </summary>
  264.         public Oglas ScrapeBasicInfo()
  265.         {
  266.             Oglas basicInfoOglas = new Oglas();
  267.             List<string> BasicInfo = new List<string>();
  268.             var Divs = Browser.Document.GetElementsByTagName("div");
  269.             foreach (HtmlElement div in Divs)
  270.             {
  271.                 string className = div.OuterHtml;
  272.                 if (className.Contains("property_details_info"))
  273.                 {
  274.                     if (div.InnerHtml != null && div.InnerHtml != "")
  275.                     {
  276.                         BasicInfo.Add(div.InnerText);
  277.                     }
  278.                 }
  279.             }
  280.  
  281.             BasicInfo.RemoveRange(0, 4);
  282.             basicInfoOglas.AddProperties(BasicInfo);
  283.            
  284.  
  285.             return basicInfoOglas;
  286.         }
  287.  
  288.         // Prva faza - Ukucaj sifru u search box :) [x]
  289.         // Druga faza - popuni BasicProperties [x]
  290.         // Treca faza - Uzmi naslov, opis, i dodatne informacije
  291.     }
  292. }
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313. using HtmlAgilityPack;
  314. using Microsoft.Win32;
  315. using OpenQA.Selenium;
  316. using OpenQA.Selenium.Chrome;
  317. using OpenQA.Selenium.PhantomJS;
  318. using OpenQA.Selenium.Support.UI;
  319. using System;
  320. using System.Collections.Generic;
  321. using System.ComponentModel;
  322. using System.Data;
  323. using System.Diagnostics;
  324. using System.Drawing;
  325. using System.Linq;
  326. using System.Text;
  327. using System.Threading;
  328. using System.Threading.Tasks;
  329. using System.Windows.Forms;
  330.  
  331. namespace NekretnineScraperTest
  332. {
  333.     public partial class Form1 : Form
  334.     {
  335.         public string currentPageOnOglasiRs = "DodajProdaju";
  336.         HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
  337.         //ChromeDriver driver = new ChromeDriver();
  338.         Scraper scraper;
  339.         bool navigated = false;
  340.         public Form1()
  341.         {
  342.             //SetBrowserFeatureControl();
  343.             InitializeComponent();
  344.             webBrowser1.ScriptErrorsSuppressed = true;
  345.             scraper = new Scraper(webBrowser1);
  346.             //// 4824efef-f41f-4969-a0af-4d6599714c75 - pozicionira se na Prodaju
  347.             //webBrowser1.Navigate("https://www.oglasi.rs/predaja-oglasa/4824efef-f41f-4969-a0af-4d6599714c75");
  348.             webBrowser1.Navigate("http://nekretnine-novisad.rs/nekretnine/stan.php");
  349.  
  350.  
  351.             //htmlDoc.OptionFixNestedTags = true;
  352.  
  353.  
  354.             //Console.Write("asdasd");
  355.             //IWebDriver driver = new ChromeDriver();
  356.             //driver.Navigate().GoToUrl("https://www.oglasi.rs/predaja-oglasa");
  357.  
  358.            
  359.             //driver.Manage().Window.Minimize();
  360.            
  361.             //driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30);
  362.             //OglasiRsAlgorithm();
  363.             //driver.Quit();
  364.  
  365.             //IWebDriver driver = new ChromeDriver();
  366.             //driver.Manage().Window.Maximize();
  367.             //driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30);
  368.             //driver.Navigate().GoToUrl("https://images.google.com/");
  369.  
  370.             //driver.FindElement(By.XPath("//*[@id='qbi']")).Click();
  371.             //driver.FindElement(By.XPath("//*[@id='qbug']/div/a")).Click();
  372.             //IWebElement fileUpload = driver.FindElement(By.XPath("//*[@id='qbfile']"));
  373.  
  374.             //fileUpload.SendKeys("D:\\Pictures\\6PM.png");
  375.  
  376.             //driver.FindElement(By.Id("username")).SendKeys("tnekr77@hotmail.com");
  377.             //driver.FindElement(By.Id("LoginPswd")).SendKeys("tnekretnine77");
  378.             //driver.FindElement(By.XPath("//*[@id='content']/div[2]/div[1]/div/div[2]/div/form/fieldset[2]/div/button")).Click();
  379.             //driver.FindElement(By.XPath("//*[@id='AdvertForm']/fieldset/div/div[2]/div/div[1]/div[1]/a")).Click();
  380.             //driver.FindElement(By.XPath("//*[@id='AdvertForm']/fieldset/div/div[2]/div/div[2]/div[1]/a/span")).Click();
  381.             //driver.FindElement(By.XPath("//*[@id='AdvertForm']/fieldset/div/div[2]/div/div[2]/div[1]")).Click();
  382.             ////Thread.Sleep(2000);
  383.             //driver.FindElement(By.XPath("//*[@id='AdvertForm']/div/fieldset[5]/div[2]/div/div[1]/span/input")).Click();
  384.  
  385.  
  386.  
  387.             // Nadji Dom - ide lagano, samo trebaju da se pokupe podaci, naprave Enumi ili dictionaryiji za svaki Dropdown, da se zna sta je vrednost i to, i slike mogu biti prob
  388.             // Realitica - problem pravi jedino prva strana, gde se bira kategorija, vrsta, drzava i to, ali dobra stvar je sto moze preko url-a da se pristupi, znaci mogu da buildujem url na osnovu parametara
  389.             // Potrebno pitati samo da li se nesto ne popunjava ili kako bi mogao lakse da automatizujem, kasnije ide opusteno (hope so...)
  390.             // Bice potreban Queue koji ce biti punjen modelima, i samo ce se cuvati sledece vreme, znaci kad se klikne submit, uzme se prvi koji treba da bude obradjen, tj najblize vreme trenutnom
  391.             // I kada dodje to vreme, ti boxovi postanu disabled i queuje se taj koji treba
  392.             // Mozda cak i da ne bude queue, nego dictionary, da su kljucevi vremena, a vrednosti Modeli koji trebaju da se objave i na koji sajt
  393.             // Bolje odvojiti na koji sajt da se objavi, nekako sklopiti to sve u strukturu (Model za objavu, vreme, koji sajt)
  394.  
  395.             // Mozda da pored svakog reda bude cancel, da se ne zajebu
  396.             // Kad se pokrene app, kada pokrenu odredjeni proces tj oglas koji treba da se svuce sa njihovog sajta, program pocinje da svlaci sve oglase koji su zadati, i strpa ih u neki txt i cuva ih u listi
  397.             // Tako da kad dodje vreme samo ih objavi iz liste, u txt se cuva zbog sigurnosnih razloga, ukoliko nestane struje i slicno.
  398.  
  399.             // Pri downloadu potrebno sacuvati sve slike u folder koji ce imati Sifru tog entiteta.
  400.  
  401.             // Napraviti funkcije, koje ce da rade sve te foreacheve, ti samo prosledis tagname i atribut koji treba da nadje, u slucaju da treba da trazi po vrednosti, imati novu funkciju
  402.  
  403.         }
  404.  
  405.         private void SetBrowserFeatureControlKey(string feature, string appName, uint value)
  406.         {
  407.             using (var key = Registry.CurrentUser.CreateSubKey(
  408.                 String.Concat(@"Software\Microsoft\Internet Explorer\Main\FeatureControl\", feature),
  409.                 RegistryKeyPermissionCheck.ReadWriteSubTree))
  410.             {
  411.                 key.SetValue(appName, (UInt32)value, RegistryValueKind.DWord);
  412.             }
  413.         }
  414.  
  415.         private void SetBrowserFeatureControl()
  416.         {
  417.             // http://msdn.microsoft.com/en-us/library/ee330720(v=vs.85).aspx
  418.  
  419.             // FeatureControl settings are per-process
  420.             var fileName = System.IO.Path.GetFileName(Process.GetCurrentProcess().MainModule.FileName);
  421.  
  422.             // make the control is not running inside Visual Studio Designer
  423.             if (String.Compare(fileName, "devenv.exe", true) == 0 || String.Compare(fileName, "XDesProc.exe", true) == 0)
  424.                 return;
  425.  
  426.             SetBrowserFeatureControlKey("FEATURE_BROWSER_EMULATION", fileName, GetBrowserEmulationMode()); // Webpages containing standards-based !DOCTYPE directives are displayed in IE10 Standards mode.
  427.             SetBrowserFeatureControlKey("FEATURE_AJAX_CONNECTIONEVENTS", fileName, 1);
  428.             SetBrowserFeatureControlKey("FEATURE_ENABLE_CLIPCHILDREN_OPTIMIZATION", fileName, 1);
  429.             SetBrowserFeatureControlKey("FEATURE_MANAGE_SCRIPT_CIRCULAR_REFS", fileName, 1);
  430.             SetBrowserFeatureControlKey("FEATURE_DOMSTORAGE ", fileName, 1);
  431.             SetBrowserFeatureControlKey("FEATURE_GPU_RENDERING ", fileName, 1);
  432.             SetBrowserFeatureControlKey("FEATURE_IVIEWOBJECTDRAW_DMLT9_WITH_GDI  ", fileName, 0);
  433.             SetBrowserFeatureControlKey("FEATURE_DISABLE_LEGACY_COMPRESSION", fileName, 1);
  434.             SetBrowserFeatureControlKey("FEATURE_LOCALMACHINE_LOCKDOWN", fileName, 0);
  435.             SetBrowserFeatureControlKey("FEATURE_BLOCK_LMZ_OBJECT", fileName, 0);
  436.             SetBrowserFeatureControlKey("FEATURE_BLOCK_LMZ_SCRIPT", fileName, 0);
  437.             SetBrowserFeatureControlKey("FEATURE_DISABLE_NAVIGATION_SOUNDS", fileName, 1);
  438.             SetBrowserFeatureControlKey("FEATURE_SCRIPTURL_MITIGATION", fileName, 1);
  439.             SetBrowserFeatureControlKey("FEATURE_SPELLCHECKING", fileName, 0);
  440.             SetBrowserFeatureControlKey("FEATURE_STATUS_BAR_THROTTLING", fileName, 1);
  441.             SetBrowserFeatureControlKey("FEATURE_TABBED_BROWSING", fileName, 1);
  442.             SetBrowserFeatureControlKey("FEATURE_VALIDATE_NAVIGATE_URL", fileName, 1);
  443.             SetBrowserFeatureControlKey("FEATURE_WEBOC_DOCUMENT_ZOOM", fileName, 1);
  444.             SetBrowserFeatureControlKey("FEATURE_WEBOC_POPUPMANAGEMENT", fileName, 0);
  445.             SetBrowserFeatureControlKey("FEATURE_WEBOC_MOVESIZECHILD", fileName, 1);
  446.             SetBrowserFeatureControlKey("FEATURE_ADDON_MANAGEMENT", fileName, 0);
  447.             SetBrowserFeatureControlKey("FEATURE_WEBSOCKET", fileName, 1);
  448.             SetBrowserFeatureControlKey("FEATURE_WINDOW_RESTRICTIONS ", fileName, 0);
  449.             SetBrowserFeatureControlKey("FEATURE_XMLHTTP", fileName, 1);
  450.         }
  451.  
  452.         private UInt32 GetBrowserEmulationMode()
  453.         {
  454.             int browserVersion = 7;
  455.             using (var ieKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Internet Explorer",
  456.                 RegistryKeyPermissionCheck.ReadSubTree,
  457.                 System.Security.AccessControl.RegistryRights.QueryValues))
  458.             {
  459.                 var version = ieKey.GetValue("svcVersion");
  460.                 if (null == version)
  461.                 {
  462.                     version = ieKey.GetValue("Version");
  463.                     if (null == version)
  464.                         throw new ApplicationException("Microsoft Internet Explorer is required!");
  465.                 }
  466.                 int.TryParse(version.ToString().Split('.')[0], out browserVersion);
  467.             }
  468.  
  469.             UInt32 mode = 11000; // Internet Explorer 11. Webpages containing standards-based !DOCTYPE directives are displayed in IE11 Standards mode. Default value for Internet Explorer 11.
  470.             switch (browserVersion)
  471.             {
  472.                 case 7:
  473.                     mode = 7000; // Webpages containing standards-based !DOCTYPE directives are displayed in IE7 Standards mode. Default value for applications hosting the WebBrowser Control.
  474.                     break;
  475.                 case 8:
  476.                     mode = 8000; // Webpages containing standards-based !DOCTYPE directives are displayed in IE8 mode. Default value for Internet Explorer 8
  477.                     break;
  478.                 case 9:
  479.                     mode = 9000; // Internet Explorer 9. Webpages containing standards-based !DOCTYPE directives are displayed in IE9 mode. Default value for Internet Explorer 9.
  480.                     break;
  481.                 case 10:
  482.                     mode = 10000; // Internet Explorer 10. Webpages containing standards-based !DOCTYPE directives are displayed in IE10 mode. Default value for Internet Explorer 10.
  483.                     break;
  484.                 default:
  485.                     // use IE11 mode by default
  486.                     break;
  487.             }
  488.  
  489.             return mode;
  490.         }
  491.  
  492.         private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
  493.         {
  494.             if (webBrowser1.Url.ToString() == "http://www.nadjidom.com/sr/login")
  495.             {
  496.                 LoginToNadjiDom();
  497.             }
  498.             else if (webBrowser1.Url.ToString() == "http://www.nadjidom.com/sr/my-real-estate-ads&action=add")
  499.             {
  500.                 if (webBrowser1.ReadyState == WebBrowserReadyState.Complete)
  501.                 {
  502.                     AddPodaciNadjiDom();
  503.                 }
  504.                
  505.             }
  506.             else if (webBrowser1.Url.ToString() == "http://nekretnine-novisad.rs/nekretnine/stan.php")
  507.             {
  508.                 if (webBrowser1.ReadyState == WebBrowserReadyState.Complete)
  509.                 {
  510.                     scraper.FindFullOglasUrl("424766");
  511.                 }
  512.             }
  513.             else if (webBrowser1.Url.ToString().Contains("nekretnina") && !navigated)
  514.             {
  515.                 if (webBrowser1.ReadyState == WebBrowserReadyState.Complete)
  516.                 {
  517.                     string urlNekretnine = webBrowser1.Url.ToString();
  518.                     string firstPart = urlNekretnine.Substring(0, 28);
  519.                     string secondPart = urlNekretnine.Substring(39);
  520.                     string fullPath = firstPart + secondPart;
  521.                     navigated = true;
  522.                     webBrowser1.Navigate(fullPath);
  523.                 }
  524.                
  525.             }
  526.             else if (!webBrowser1.Url.ToString().Contains("/nekretnine/nekretnina/") && navigated)
  527.             {
  528.                 if (webBrowser1.ReadyState == WebBrowserReadyState.Complete)
  529.                 {
  530.                     Oglas basicInfoOglas = scraper.ScrapeBasicInfo();
  531.                 }
  532.                
  533.             }
  534.             //else if (webBrowser1.Url.ToString() == "https://www.oglasi.rs/oglasi/nekretnine")
  535.             //{
  536.             //    if (webBrowser1.ReadyState == WebBrowserReadyState.Complete)
  537.             //    {
  538.             //        var elements = webBrowser1.Document.GetElementsByTagName("a");
  539.             //        foreach (HtmlElement element in elements)
  540.             //        {
  541.             //            if (element.InnerHtml.Contains("Dodaj oglas"))
  542.             //            {
  543.             //                element.InvokeMember("Click");
  544.             //                currentPageOnOglasiRs = "DodajProdaju";
  545.             //                break;
  546.             //            }
  547.             //        }
  548.             //    }
  549.             //}
  550.             //else if (webBrowser1.Url.ToString().Contains("https://www.oglasi.rs/predaja-oglasa"))
  551.             //{
  552.             //    htmlDoc.LoadHtml(webBrowser1.DocumentText);
  553.             //    HtmlNode node = htmlDoc.DocumentNode.SelectSingleNode("//*[@id='AdvertForm']/fieldset/div/div[2]/div/div[2]/div[1]/a");
  554.  
  555.             //    Console.Write("asdasd");
  556.             //    //if (webBrowser1.ReadyState == WebBrowserReadyState.Complete)
  557.             //    //{
  558.             //    //    //AddPodaciOglasiRs();
  559.             //    //    InjectRawOglasiRs();
  560.             //    //}
  561.             //}
  562.  
  563.         }
  564.  
  565.         //private void InjectRawOglasiRs()
  566.         //{
  567.         //    var elements = webBrowser1.DocumentText;
  568.         //    //    foreach (HtmlElement item in elements)
  569.         //    //    {
  570.         //    //        if (item.InnerHtml.Contains("getCategoryTitle"))
  571.         //    //        {
  572.         //    //            item.InnerHtml = "Stanova (Nekretnine, Prodaja)";
  573.         //    //            break;
  574.         //    //        }
  575.         //    //    }
  576.         //    Console.WriteLine("desi");
  577.         //}
  578.  
  579.         //private void AddPodaciOglasiRs()
  580.         //{
  581.         //    var elements = webBrowser1.Document.GetElementsByTagName("label");
  582.         //    //if (currentPageOnOglasiRs == "DodajProdaju")
  583.         //    //{
  584.         //    //    foreach (HtmlElement element in elements)
  585.         //    //    {
  586.         //    //        if (currentPageOnOglasiRs == "DodajProdaju")
  587.         //    //        {
  588.         //    //            if (element.InnerHtml.Contains("Prodaja"))
  589.         //    //            {
  590.         //    //                currentPageOnOglasiRs = "DodajStan";
  591.         //    //                element.InvokeMember("Click");
  592.         //    //                break;
  593.         //    //            }
  594.         //    //        }
  595.         //    //    }
  596.         //    //}
  597.         //    //var elementsRefreshed = webBrowser1.Document.GetElementsByTagName("a");
  598.         //    foreach (HtmlElement element in elements)
  599.         //    {
  600.                
  601.         //        if (element.InnerHtml != null && element.InnerHtml.Contains("stanova"))
  602.         //        {
  603.         //            element.Children[1].InvokeMember("click");
  604.         //            break;
  605.         //        }
  606.         //    }
  607.  
  608.         //}
  609.  
  610.         //private void OglasiRsAlgorithm()
  611.         //{
  612.         //    driver.Url = "https://www.oglasi.rs/predaja-oglasa/4824efef-f41f-4969-a0af-4d6599714c75";
  613.         //    driver.Navigate();
  614.         //    var source = driver.PageSource;
  615.         //    // Uloguj se
  616.         //    driver.FindElement(By.Id("username")).SendKeys("tnekr77@hotmail.com");
  617.         //    driver.FindElement(By.Id("LoginPswd")).SendKeys("tnekretnine77");
  618.         //    driver.FindElement(By.XPath("//*[@id='content']/div[2]/div[1]/div/div[2]/div/form/fieldset[2]/div/button")).Click();
  619.            
  620.         //    // Idi na Stanovi (nekretnine)
  621.         //    driver.FindElementByXPath("//*[@id='AdvertForm']/fieldset/div/div[2]/div/div[2]/div[1]/a").Click();
  622.         //    driver.Manage().Window.Maximize();
  623.  
  624.         //    // Biranje lokacije
  625.         //    driver.FindElementByXPath("//*[@id='location_selection_anchor']/div/div[2]/div[1]/div/span/button[2]").Click();
  626.  
  627.         //    // Odaberi Novi sad
  628.         //    driver.FindElementByXPath("//*[@id='location_selection_anchor']/div/div[2]/div[1]/div/div/div/div/ul/li[98]/a").Click();
  629.  
  630.         //    // Odaberi Budisavu
  631.         //    driver.FindElementByXPath("//*[@id='location_selection_anchor']/div/div[2]/div[1]/div/div/div/div/ul/li[2]/a").Click();
  632.  
  633.         //    // Odaberi sobnost
  634.         //    var Sobnost = driver.FindElementById("AdvertForm_fldsobnost");
  635.         //    var selectedElement = new SelectElement(Sobnost);
  636.         //    Thread.Sleep(1000);
  637.         //    selectedElement.SelectByText("Jednosoban");
  638.         //}
  639.  
  640.         private void UploadPhotoTest()
  641.         {
  642.             var elements = webBrowser1.Document.GetElementsByTagName("input");
  643.             foreach (HtmlElement element in elements)
  644.             {
  645.                 // If there's more than one button, you can check the
  646.                 //element.InnerHTML to see if it's the one you want
  647.                 var type = element.GetAttribute("type").ToString();
  648.                 if (type == "file")
  649.                 {
  650.                     timer1.Interval = 3000;
  651.                     timer1.Start();
  652.                     element.InvokeMember("click");
  653.                 }
  654.             }
  655.         }
  656.  
  657.         private void LoginToNadjiDom()
  658.         {
  659.             webBrowser1.Document.GetElementById("login_username").InnerText = "tnekr77@hotmail.com";
  660.             webBrowser1.Document.GetElementById("login_password").InnerText = "tnekretnine77";
  661.  
  662.             webBrowser1.Document
  663.                        .GetElementsByTagName("input")
  664.                        .GetElementsByName("login")[0]
  665.                        .InvokeMember("click");
  666.         }
  667.  
  668.         private void AddPodaciNadjiDom()
  669.         {
  670.             webBrowser1.Document.GetElementById("id_ad_type").SetAttribute("value", "1");
  671.             webBrowser1.Document.GetElementById("price").InnerText = "2000";
  672.         }
  673.  
  674.         private void timer1_Tick(object sender, EventArgs e)
  675.         {
  676.             timer1.Stop();
  677.             SendKeys.SendWait("\"D:\\Pictures\\6PM.png\" \"D:\\Pictures\\6bas.jpg\"");
  678.             SendKeys.SendWait("{TAB 2}");
  679.             SendKeys.SendWait("{ENTER}");
  680.         }
  681.  
  682.         private void button1_Click(object sender, EventArgs e)
  683.         {
  684.             //driver.Quit();
  685.         }
  686.     }
  687. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement