※ 개인의 호기심을 해결하기 위해 기능 구현만 함. 저작권은 항상 조심.
1. .NET 5
2. 셀레니움(3.141.0)
3. Microsoft Edge
- 참고 1 : WebDriver (Chromium) - Microsoft Edge Development | Microsoft Docs
WebDriver (Chromium) - Microsoft Edge Development
Learn how to test your website or app in Microsoft Edge or automate the browser with WebDriver.
docs.microsoft.com
- 참고 2 : WebDriver - Microsoft Edge Developer
WebDriver - Microsoft Edge Developer
Microsoft Edge Legacy Microsoft WebDriver for Microsoft Edge Legacy versions 18 and 19 is a Windows Feature on Demand which ensures that it’s always up to date automatically and enables some new ways to get Microsoft WebDriver. To get started you will ha
developer.microsoft.com
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Edge.SeleniumTools;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
using SeleniumExtras.WaitHelpers;
namespace KakaoPage_Crawling_test
{
public partial class Form1 : Form
{
EdgeDriver edgeDriver = null;
public Form1()
{
InitializeComponent();
button1.Click += btn_Login;
#if DEBUG
textBox1.Text = "ididid@ididid.com";
textBox2.Text = "pwpwpwp";
#endif
}
private void btn_Login(object sender, EventArgs e)
{
string 카카오id = string.Empty;
string 카카오pw = string.Empty;
if (string.IsNullOrEmpty(textBox1.Text))
{
textBox1.Focus();
return;
}
else
카카오id = textBox1.Text;
if (string.IsNullOrEmpty(textBox2.Text))
{
textBox2.Focus();
return;
}
else
카카오pw = textBox2.Text;
if (edgeDriver is null)
{
var options = new EdgeOptions();
options.UseChromium = true;
options.PageLoadStrategy = PageLoadStrategy.Eager;
edgeDriver = new EdgeDriver(options);
// 카카오페이지 메인 화면
edgeDriver.Url = "https://page.kakao.com/main";
// 로그인 버튼 클릭
var login = edgeDriver.FindElementByClassName("css-mj3zj6");
login.Click();
// 팝업창 변경
edgeDriver.SwitchTo().Window(edgeDriver.WindowHandles[1]);
edgeDriver.FindElementById("id_email_2").SendKeys(카카오id);
edgeDriver.FindElementById("id_password_3").SendKeys(카카오pw);
// 로그인 클릭
edgeDriver.FindElementByXPath("/html/body/div[1]/div[2]/div/div/div/div/div[2]/div/form/fieldset/div[8]/button[1]").Click();
// 메인 윈도우로 변경
edgeDriver.SwitchTo().Window(edgeDriver.WindowHandles[0]);
bool continues = false;
while (true)
{
// 캐시충전 글자가 없어질때까지 반복 및
if (!edgeDriver.PageSource.Contains("캐시충전"))
{
edgeDriver.Navigate().GoToUrl("https://page.kakao.com/home?seriesId=대상id");
continues = true;
break;
}
Thread.Sleep(1);
}
if (continues)
{
continues = false;
// 제목 표시
textBox3.Text += edgeDriver.FindElementByClassName("css-4cffwv").Text.Trim() + Environment.NewLine;
Thread.Sleep(3000);
// 총 횟수
textBox3.Text += edgeDriver.FindElementByClassName("css-1qe6yee").Text.Trim() + Environment.NewLine;
while (true)
{
if (edgeDriver.PageSource.Contains("첫편보기"))
{
continues = true;
break;
}
Thread.Sleep(1);
}
if (continues)
{
continues = false;
// 첫편보기 클릭
edgeDriver.FindElementByXPath("html/body/div[1]/div/div[3]/div/div/div[1]/div/div[2]/div[3]/div[2]/button[2]").Click();
// url이 변경 될 때까지 기다리기
while (!edgeDriver.Url.Contains("https://page.kakao.com/viewer?productId="))
{
Thread.Sleep(1);
}
Thread.Sleep(3000);
// 처음 화는 한번 클릭을 해야 화살표가 사라지기 때문에 body부분 클릭하게 함.
new WebDriverWait(edgeDriver, TimeSpan.FromSeconds(3)).Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.XPath("/html/body")));
new WebDriverWait(edgeDriver, TimeSpan.FromSeconds(3)).Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(By.XPath("/html/body"))).Click();
// 한 화의 횟 수
int pageCount = 0;
string sPage = new WebDriverWait(edgeDriver, TimeSpan.FromSeconds(10)).Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.XPath("/html/body/div[1]/div/div[3]/div/div/div/div[2]"))).Text.Trim();
// ex) 1 / 28
sPage = sPage.Substring(sPage.IndexOf('/') + 1, sPage.Length - sPage.IndexOf('/') - 1).Trim();
// ex) 28
textBox3.Text += sPage + Environment.NewLine;
pageCount = Convert.ToInt32(sPage);
// 이미지 src 구하기
string imgSrc = new WebDriverWait(edgeDriver, TimeSpan.FromSeconds(5)).Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.XPath("/html/body/div[1]/div/div[3]/div/div/div/div[1]/img"))).GetAttribute("src");
textBox3.Text += "src = " + imgSrc + Environment.NewLine;
string targetPath = @"C:\Users\Desktop\img\";
using (var webClient = new WebClient())
{
// 비동기 다운로드
webClient.DownloadFileAsync(new Uri(imgSrc), targetPath + "1.png");
}
}
}
}
}
}
}
'C#' 카테고리의 다른 글
[C#] CPU, RAM, HDD 사용량 및 OS Version, Last boottime 확인 (0) | 2021.02.10 |
---|---|
[C#] 공공 데이터 오픈 API 사용(제대 군인 채용 정보 얻기) (0) | 2021.01.02 |
[C#] gRPC 와 LiteDB 사용 (0) | 2020.12.15 |
[C#] TCP Listener을 이용한 비동기 (2) | 2020.12.04 |
[C#] UDP Socket을 이용한 통신 (2) | 2020.12.04 |