Advertisement
Guest User

Untitled

a guest
Jan 10th, 2018
571
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.46 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Data.SqlClient;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. using OpenQA.Selenium;
  12. using OpenQA.Selenium.Support.UI;
  13.  
  14. namespace WindowsFormsApplication1
  15. {
  16.  
  17.     public partial class Form1 : Form
  18.     {
  19.         IWebDriver Browser;
  20.      
  21.  
  22.         public Form1()
  23.         {
  24.             InitializeComponent();
  25.         }
  26.        
  27.         private void button1_Click(object sender, EventArgs e)
  28.         {
  29.            
  30.             Browser.Navigate().GoToUrl("https://www.instagram.com/explore/");
  31.             IWebElement SearchInput = Browser.FindElement(By.TagName("input"),10);
  32.             string Tag = HashTagText.Text;
  33.             SearchInput.SendKeys(Tag);
  34.             System.Threading.Thread.Sleep(3000);
  35.             SearchInput.SendKeys(OpenQA.Selenium.Keys.Enter);
  36.            
  37.         }
  38.  
  39.         private void button2_Click(object sender, EventArgs e)
  40.         {
  41.             Browser.Quit();
  42.         }
  43.  
  44.         private void button4_Click(object sender, EventArgs e)
  45.         {
  46.             Browser = new OpenQA.Selenium.Chrome.ChromeDriver();
  47.             Browser.Manage().Window.Maximize();
  48.             Browser.Navigate().GoToUrl("https://www.instagram.com");
  49.            
  50.             IWebElement Login = Browser.FindElement(By.XPath(@"//*[@id="+"\""+@"react-root" + "\""+@"]/section/main/article/div[2]/div[2]/p/a"),10);
  51.             Login.Click();
  52.             IWebElement UserName = Browser.FindElement(By.Name("username"),10);
  53.             // UserName.SendKeys("Instagram20_17@mail.ru");
  54.             SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-I33KJCN\SQLEXPRESS;Initial Catalog=insta_bot;Integrated Security=True");
  55.             con.Open();
  56.             string query = "SELECT login,password FROM USERS where id = 2";
  57.             SqlCommand command = new SqlCommand(query,con);
  58.             SqlDataReader reader =  command.ExecuteReader();
  59.             string pc = null;
  60.             string pc1 = null;
  61.             while (reader.Read())
  62.             {
  63.                pc = reader[0].ToString();
  64.                pc1 = reader[1].ToString();
  65.             }
  66.          
  67.             UserName.SendKeys(pc);
  68.             IWebElement Password = Browser.FindElement(By.Name("password"),10);
  69.             //Password.SendKeys("6qPdsfDcGy4fm3WVEKaV" + OpenQA.Selenium.Keys.Enter);
  70.                        Password.SendKeys(pc1 + OpenQA.Selenium.Keys.Enter);
  71.             con.Close();
  72.         }
  73.  
  74.         private void button3_Click(object sender, EventArgs e)
  75.         {
  76.             backgroundWorker1.CancelAsync();
  77.         }
  78.  
  79.         private void button5_Click(object sender, EventArgs e)
  80.         {
  81.  
  82.             backgroundWorker1.RunWorkerAsync();
  83.         }
  84.  
  85.         public void Subcribe (object sender, DoWorkEventArgs e)
  86.         {
  87.  
  88.             // IWebElement FirstElement = Browser.FindElement(By.CssSelector("._nljxa ._ovg3g"),10);
  89.             IWebElement FirstElement = Browser.FindElement(By.XPath (@"//*[@id="+"\""+@"react-root"+"\""+@"]/section/main/article/div[2]/div[1]/div[1]/a[1]/div/div[2]"), 10);
  90.                                                                            //*[@id="react-root"]/section/main/article/div[2]/div[1]/div[1]/a[1]/div/div[2]
  91.             FirstElement.Click();
  92.             Wait(500);
  93.             //int Count = Decimal.
  94.            
  95.             for (int i = 0; i < Decimal.ToInt32(SubscribeCountEnter.Value); i++)
  96.             {
  97.                 if (backgroundWorker1.CancellationPending) return;
  98.                
  99.                 if(!SubscribeAction())i--;
  100.                 LikeAction();
  101.                 NextAction();
  102.             }
  103.             IWebElement Close = Browser.FindElement(By.CssSelector("button._3eajp"),10);
  104.             Close.Click();
  105.             Wait(500);
  106.             MessageBox.Show("Мы подписались на " + SubscribeCountEnter.Value + " аккаунтов");
  107.         }
  108.      
  109.  
  110.         public Boolean SubscribeAction()
  111.         {
  112.             IWebElement g = Browser.FindElement(By.CssSelector("._fbms8 ._ah57t"),10);
  113.             Wait(500);
  114.             if (g.Text == "Подписаться")
  115.             {
  116.                 g.Click();
  117.                 Wait(1000);
  118.                 return true;
  119.  
  120.             }
  121.             else
  122.             {
  123.                 Wait(1000);
  124.                 return false;
  125.             }
  126.            
  127.         }
  128.  
  129.         public void LikeAction()
  130.         {
  131.             IWebElement LikeBtn = Browser.FindElement(By.CssSelector("section._jveic  a ._soakw"), 10);
  132.             if (LikeBtn.Text == "Нравится")
  133.             {
  134.                 LikeBtn.Click();
  135.             }
  136.             Wait(700);
  137.            
  138.         }
  139.  
  140.         public void NextAction()
  141.         {
  142.             IWebElement NextBtn = Browser.FindElement(By.LinkText("Далее"), 10);
  143.             NextBtn.Click();
  144.         }
  145.  
  146.         public void Wait(int f)
  147.         {
  148.              System.Threading.Thread.Sleep(f);
  149.         }
  150.  
  151.         private void button6_Click(object sender, EventArgs e)
  152.         {
  153.             // Cookie cookie = new Cookie("key", "value");
  154.             //Browser.Manage().Cookies.AddCookie(cookie);
  155.             SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-I33KJCN\SQLEXPRESS;Initial Catalog=insta_bot;Integrated Security=True");
  156.             con.Open();
  157.  
  158.             var allCookies = Browser.Manage().Cookies.AllCookies;
  159.             foreach (Cookie cookie in allCookies)
  160.             {
  161.                 string str = cookie.ToString();
  162.                 //MessageBox.Show(str, "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information);
  163.                 string query = "INSERT INTO [User_Cookie] (user_id, cookie) VALUES(2, @cookie)";
  164.                 SqlCommand command = new SqlCommand(query, con);
  165.                 command.Parameters.AddWithValue("cookie", str);
  166.                 command.ExecuteNonQuery();
  167.             }
  168.             con.Close();
  169.  
  170.         }
  171.  
  172.  
  173.  
  174.  
  175.         /* private void Sell(object sender, DoWorkEventArgs e)
  176.          {
  177.  
  178.  
  179.              IWebElement SteamContainer = Browser.FindElement(By.ClassName("inventory_ctn"));
  180.  
  181.              int CardsProcessed = 0;
  182.              bool FirstSell = true;
  183.  
  184.  
  185.              List<IWebElement> Items = SteamContainer.FindElements(By.ClassName("itemHolder")).ToList();
  186.              for (int i = 0; i < Items.Count; i++)
  187.              {
  188.  
  189.                  if (backgroundWorker1.CancellationPending) return;
  190.  
  191.                  if (Items[i].GetAttribute("style") == "")
  192.                  {
  193.                      System.Threading.Thread.Sleep(2000);
  194.                      Items[i].Click();
  195.  
  196.                      String DivText = "";
  197.                      IWebElement SellBtn = null;
  198.  
  199.                      System.Threading.Thread.Sleep(1000);
  200.                      List<IWebElement> SellDivs = Browser.FindElements(By.ClassName("item_market_actions")).ToList();
  201.                      for (int j = 0; j < SellDivs.Count; j++)
  202.                      {
  203.                          if (SellDivs[j].Displayed)
  204.                          {
  205.                              DivText = SellDivs[j].Text;
  206.                              SellBtn = SellDivs[j].FindElement(By.CssSelector(".item_market_action_button.item_market_action_button_green"));
  207.                          }
  208.                      }
  209.  
  210.                      String priceStr = System.Text.RegularExpressions.Regex.Match(DivText, @"[0-9]+\,?[0-9]*").Value;
  211.                      float Price = Single.Parse(priceStr) + (float)nudPriceChange.Value;
  212.  
  213.  
  214.                      Actions SellClick = new Actions(Browser);
  215.                      SellClick.MoveToElement(SellBtn).Click().Perform();
  216.  
  217.                      //SellBtn.Click();
  218.                      System.Threading.Thread.Sleep(2000);
  219.  
  220.                      if (FirstSell)
  221.                      {
  222.                          Browser.FindElement(By.Id("market_sell_dialog_accept_ssa")).Click();
  223.                          FirstSell = false;
  224.                      }
  225.  
  226.                      Browser.FindElement(By.Id("market_sell_buyercurrency_input")).SendKeys(Price.ToString());
  227.                      System.Threading.Thread.Sleep(1000);
  228.                      Browser.FindElement(By.Id("market_sell_dialog_accept")).Click();
  229.                      System.Threading.Thread.Sleep(1000);
  230.                      Browser.FindElement(By.Id("market_sell_dialog_ok")).Click();
  231.  
  232.                      System.Threading.Thread.Sleep(3000);
  233.  
  234.  
  235.  
  236.  
  237.                      Browser.FindElement(By.CssSelector(".newmodal_buttons .btn_grey_white_innerfade.btn_medium")).Click();
  238.  
  239.                      CardsProcessed++;
  240.                      if (CardsProcessed == 25)
  241.                      {
  242.                          CardsProcessed = 0;
  243.                          Browser.FindElement(By.Id("pagebtn_next")).Click();
  244.                          System.Threading.Thread.Sleep(2000);
  245.                      }
  246.                  }
  247.              }
  248.  
  249.  
  250.  
  251.          }*/
  252.     }
  253.  
  254.     public static class WebDriverExtensions
  255.     {
  256.         public static IWebElement FindElement(this IWebDriver Browser, By by, int timeoutInSeconds)
  257.         {
  258.             if (timeoutInSeconds > 0)
  259.             {
  260.                 var wait = new WebDriverWait(Browser, TimeSpan.FromSeconds(timeoutInSeconds));
  261.                 return wait.Until(Brs => Brs.FindElement(by));
  262.             }
  263.             return Browser.FindElement(by);
  264.         }
  265.     }
  266. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement