1. 링크 클릭 및 익셉션 제거
This commit is contained in:
@@ -1,8 +1,10 @@
|
|||||||
using OpenQA.Selenium;
|
using OpenQA.Selenium;
|
||||||
using OpenQA.Selenium.Chrome;
|
using OpenQA.Selenium.Chrome;
|
||||||
|
using OpenQA.Selenium.Interactions;
|
||||||
using OpenQA.Selenium.Support.UI;
|
using OpenQA.Selenium.Support.UI;
|
||||||
using SeleniumExtras.WaitHelpers;
|
using SeleniumExtras.WaitHelpers;
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Collections.ObjectModel;
|
using System.Collections.ObjectModel;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Windows.Forms;
|
using System.Windows.Forms;
|
||||||
@@ -13,6 +15,8 @@ namespace NaverSearcher
|
|||||||
{
|
{
|
||||||
ChromeDriver _ChromeDriver;
|
ChromeDriver _ChromeDriver;
|
||||||
|
|
||||||
|
Random m_Random = new Random();
|
||||||
|
|
||||||
public Form1()
|
public Form1()
|
||||||
{
|
{
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
@@ -126,25 +130,8 @@ namespace NaverSearcher
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
/*
|
|
||||||
_ChromeDriver.execute_script('window.open("about:blank", "_blank");')
|
|
||||||
_ChromeDriver.execute_script('window.open("about:blank", "_blank");')
|
|
||||||
|
|
||||||
|
|
||||||
tabs = _ChromeDriver.window_handles
|
|
||||||
|
|
||||||
# TAB_1
|
|
||||||
_ChromeDriver.switch_to_window(tabs[0])
|
|
||||||
_ChromeDriver.get('http://www.naver.com/')
|
|
||||||
|
|
||||||
# TAB_2
|
|
||||||
_ChromeDriver.switch_to_window(tabs[1])
|
|
||||||
_ChromeDriver.get('http://www.google.com/')
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
WebDriverWait _WebDriverWait = new WebDriverWait(_ChromeDriver, TimeSpan.FromSeconds(3));
|
WebDriverWait _WebDriverWait = new WebDriverWait(_ChromeDriver, TimeSpan.FromSeconds(3));
|
||||||
var rand = new Random();
|
|
||||||
string[][] key_word =
|
string[][] key_word =
|
||||||
{
|
{
|
||||||
new string[] {"바나나우유 라떼", "초코우유 라떼" },
|
new string[] {"바나나우유 라떼", "초코우유 라떼" },
|
||||||
@@ -158,48 +145,98 @@ namespace NaverSearcher
|
|||||||
// 네이버 메인
|
// 네이버 메인
|
||||||
// 스크롤 내린다.
|
// 스크롤 내린다.
|
||||||
// 20 ~ 60초 대기
|
// 20 ~ 60초 대기
|
||||||
|
//
|
||||||
|
|
||||||
foreach (string key in key_pair)
|
try
|
||||||
{
|
{
|
||||||
// 검색어 입력 (검색어 1)
|
|
||||||
// 검색어 확인
|
|
||||||
// 스크롤 내린다.
|
|
||||||
// 20 ~ 60초 대기
|
|
||||||
// 아무 게시글 클릭
|
|
||||||
// 이동된 페이지 작업
|
|
||||||
// 스크롤 내리기
|
|
||||||
// 20 ~ 60초 대기
|
|
||||||
// 새창 닫기
|
|
||||||
|
|
||||||
_ChromeDriver.Navigate().GoToUrl(@"https://naver.com");
|
_ChromeDriver.Navigate().GoToUrl(@"https://naver.com");
|
||||||
|
_WebDriverWait.Until(ExpectedConditions.ElementIsVisible(By.XPath("//*[@id=\"query\"]")));
|
||||||
|
|
||||||
string _search_xpath = "//*[@id=\"query\"]";
|
string _logout = @"/html/body/div/div/div[1]/div[1]/a[1]";
|
||||||
_WebDriverWait.Until(ExpectedConditions.ElementIsVisible(By.XPath(_search_xpath)));
|
|
||||||
|
|
||||||
var _elment = _ChromeDriver.FindElement(By.XPath(_search_xpath));
|
if (IsElementPresent(_ChromeDriver, By.XPath(_logout)))
|
||||||
|
|
||||||
foreach (var item in HangulString.CharacterMakeHistory(key))
|
|
||||||
{
|
{
|
||||||
_elment.Clear();
|
_ChromeDriver.FindElement(By.XPath(_logout)).Click();
|
||||||
_elment.SendKeys(item);
|
|
||||||
Delay(rand.Next(500, 1000));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_elment.SendKeys(OpenQA.Selenium.Keys.Enter);
|
foreach (string key in key_pair)
|
||||||
|
{
|
||||||
|
// 검색어 입력 (검색어 1)
|
||||||
|
// 검색어 확인
|
||||||
|
// 스크롤 내린다.
|
||||||
|
// 20 ~ 60초 대기
|
||||||
|
// 아무 게시글 클릭
|
||||||
|
// 이동된 페이지 작업
|
||||||
|
// 스크롤 내리기
|
||||||
|
// 20 ~ 60초 대기
|
||||||
|
// 새창 닫기
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_ChromeDriver.Navigate().GoToUrl(@"https://naver.com");
|
||||||
|
|
||||||
((IJavaScriptExecutor)_ChromeDriver).ExecuteScript("window.scrollBy(0, document.body.scrollHeight);");
|
string _query_xpath = "//*[@id=\"query\"]";
|
||||||
Delay(rand.Next(2000, 6000));
|
|
||||||
|
|
||||||
ReadOnlyCollection<IWebElement> links = _ChromeDriver.FindElements(By.TagName("a"));
|
_WebDriverWait.Until(ExpectedConditions.ElementIsVisible(By.XPath(_query_xpath)));
|
||||||
|
|
||||||
links[rand.Next(0, links.Count)].Click();
|
IWebElement _query_elment = _ChromeDriver.FindElement(By.XPath(_query_xpath));
|
||||||
|
|
||||||
((IJavaScriptExecutor)_ChromeDriver).ExecuteScript("window.scrollBy(0, document.body.scrollHeight);");
|
foreach (var item in HangulString.CharacterMakeHistory(key))
|
||||||
Delay(rand.Next(2000, 6000));
|
{
|
||||||
|
_query_elment.Clear();
|
||||||
|
_query_elment.SendKeys(item);
|
||||||
|
Delay(m_Random.Next(500, 1000));
|
||||||
|
}
|
||||||
|
|
||||||
|
_query_elment.SendKeys(OpenQA.Selenium.Keys.Enter);
|
||||||
|
|
||||||
|
((IJavaScriptExecutor)_ChromeDriver).ExecuteScript("window.scrollBy(0, document.body.scrollHeight);");
|
||||||
|
Delay(m_Random.Next(2000, 6000));
|
||||||
|
|
||||||
|
ReadOnlyCollection<IWebElement> links = _ChromeDriver.FindElements(By.CssSelector("a[href*='https://']"));
|
||||||
|
|
||||||
|
int _try = links.Count;
|
||||||
|
|
||||||
|
do
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
int _sel = m_Random.Next(0, links.Count);
|
||||||
|
string kknd = links[_sel].GetAttribute("href");
|
||||||
|
links[_sel].Click();
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
} while (--_try > 0);
|
||||||
|
|
||||||
|
((IJavaScriptExecutor)_ChromeDriver).ExecuteScript("window.scrollBy(0, document.body.scrollHeight);");
|
||||||
|
Delay(m_Random.Next(2000, 6000));
|
||||||
|
|
||||||
|
if (_ChromeDriver.WindowHandles.First() != _ChromeDriver.WindowHandles.Last())
|
||||||
|
{
|
||||||
|
_ChromeDriver.SwitchTo().Window(_ChromeDriver.WindowHandles.Last());
|
||||||
|
_ChromeDriver.Close();
|
||||||
|
_ChromeDriver.SwitchTo().Window(_ChromeDriver.WindowHandles.First());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine("=== 검색어 ===");
|
||||||
|
Console.WriteLine(ex.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine("=== 검색어 페어 ===");
|
||||||
|
Console.WriteLine(ex.Message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return;
|
/*
|
||||||
|
|
||||||
var elem = _ChromeDriver.FindElementByXPath("//*");
|
var elem = _ChromeDriver.FindElementByXPath("//*");
|
||||||
var source_code = elem.GetAttribute("outerHTML");
|
var source_code = elem.GetAttribute("outerHTML");
|
||||||
@@ -297,10 +334,12 @@ namespace NaverSearcher
|
|||||||
|
|
||||||
//_ChromeDriver.FindElement(By.XPath(_search_xpath)).SendKeys(@"인플 검색");
|
//_ChromeDriver.FindElement(By.XPath(_search_xpath)).SendKeys(@"인플 검색");
|
||||||
|
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
MessageBox.Show(ex.Message);
|
Console.WriteLine("=== 실행 ===");
|
||||||
|
Console.WriteLine(ex.Message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -315,27 +354,32 @@ namespace NaverSearcher
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static DateTime Delay(int MS)
|
private static DateTime Delay(int MS)
|
||||||
|
|
||||||
{
|
{
|
||||||
|
|
||||||
DateTime ThisMoment = DateTime.Now;
|
DateTime ThisMoment = DateTime.Now;
|
||||||
|
|
||||||
TimeSpan duration = new TimeSpan(0, 0, 0, 0, MS);
|
TimeSpan duration = new TimeSpan(0, 0, 0, 0, MS);
|
||||||
|
|
||||||
DateTime AfterWards = ThisMoment.Add(duration);
|
DateTime AfterWards = ThisMoment.Add(duration);
|
||||||
|
|
||||||
while (AfterWards >= ThisMoment)
|
while (AfterWards >= ThisMoment)
|
||||||
|
|
||||||
{
|
{
|
||||||
|
|
||||||
System.Windows.Forms.Application.DoEvents();
|
System.Windows.Forms.Application.DoEvents();
|
||||||
|
|
||||||
ThisMoment = DateTime.Now;
|
ThisMoment = DateTime.Now;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return DateTime.Now;
|
return DateTime.Now;
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool IsElementPresent(IWebDriver _IWebDriver, By by)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_IWebDriver.FindElement(by);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
catch (NoSuchElementException)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user