回答編集履歴
1
見直しキャンペーン中
test
CHANGED
@@ -1,103 +1,52 @@
|
|
1
1
|
[リファレンス](http://www.puppeteersharp.com/api/index.html)を見ながら勘で書いたので「間違っている・もっといい方法がある」かもしれません。
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
```
|
3
|
+
```cs
|
6
|
-
|
7
4
|
using System;
|
8
|
-
|
9
5
|
using System.Threading.Tasks;
|
10
|
-
|
11
6
|
using PuppeteerSharp;
|
12
7
|
|
13
|
-
|
14
|
-
|
15
8
|
namespace Questions278339
|
16
|
-
|
17
9
|
{
|
18
|
-
|
19
10
|
class Program
|
20
|
-
|
21
11
|
{
|
22
|
-
|
23
12
|
static async Task Main()
|
24
|
-
|
25
13
|
{
|
26
|
-
|
27
14
|
await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);
|
28
|
-
|
29
15
|
var browser = await Puppeteer.LaunchAsync(new LaunchOptions
|
30
|
-
|
31
16
|
{
|
32
|
-
|
33
17
|
//Headless = false,
|
34
|
-
|
35
18
|
});
|
36
19
|
|
37
|
-
|
38
|
-
|
39
20
|
var page = await browser.NewPageAsync();
|
40
|
-
|
41
21
|
await page.GoToAsync("https://teratail.com/");
|
42
22
|
|
43
|
-
|
44
|
-
|
45
23
|
// フッターの「teratailを一緒に作りたいエンジニア」リンク
|
46
|
-
|
47
24
|
var element = await page.WaitForSelectorAsync("#footerTop > div > div.L-footer__navigation.leftCol > div.U-clearfix.clearFix > div > p > a");
|
48
|
-
|
49
25
|
await element.ClickAsync();
|
50
26
|
|
51
|
-
|
52
|
-
|
53
27
|
var url = await element.EvaluateFunctionAsync<string>("e => e.getAttribute('href')");
|
54
|
-
|
55
28
|
var target = await page.BrowserContext.WaitForTargetAsync((x) => x.Url == url);
|
56
|
-
|
57
29
|
var page2 = await target.PageAsync();
|
58
30
|
|
59
|
-
|
60
|
-
|
61
31
|
// DOMContentLoadedイベント ClickAsync後 遷移が早いと間に合わないか??
|
62
|
-
|
63
32
|
page2.DOMContentLoaded += Page2_DOMContentLoaded;
|
64
33
|
|
65
|
-
|
66
|
-
|
67
34
|
// セレクタで待つなら 「応募する」ボタン
|
68
|
-
|
69
35
|
await page2.WaitForSelectorAsync("body > section > div:nth-child(2) > div.button-area > a");
|
70
|
-
|
71
36
|
Console.WriteLine(await page2.GetContentAsync());
|
72
37
|
|
73
|
-
|
74
|
-
|
75
38
|
Console.ReadLine();
|
76
|
-
|
77
39
|
browser.Dispose();
|
78
|
-
|
79
40
|
}
|
80
41
|
|
81
|
-
|
82
|
-
|
83
42
|
private static async void Page2_DOMContentLoaded(object sender, EventArgs e)
|
84
|
-
|
85
43
|
{
|
86
|
-
|
87
44
|
if(sender is Page page)
|
88
|
-
|
89
45
|
{
|
90
|
-
|
91
46
|
page.DOMContentLoaded -= Page2_DOMContentLoaded;
|
92
|
-
|
93
47
|
Console.WriteLine(await page.GetContentAsync());
|
94
|
-
|
95
48
|
}
|
96
|
-
|
97
49
|
}
|
98
|
-
|
99
50
|
}
|
100
|
-
|
101
51
|
}
|
102
|
-
|
103
52
|
```
|