質問編集履歴
1
反映されていなかった部分を追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,5 +1,82 @@
|
|
1
1
|
msedgedriverを使用してGetActiveElementなどでElementIDを取得した際に
|
2
|
-
それがHTML上のどの要素なのかを特定
|
2
|
+
それがHTML上のどの要素なのかを特定したいです。
|
3
3
|
|
4
4
|
EdgeのデベロッパツールでIDを検索かけてみたりしたのですが
|
5
|
-
見つけられません
|
5
|
+
見つけられません
|
6
|
+
|
7
|
+
方法はありませんか?
|
8
|
+
|
9
|
+
```PowerShell
|
10
|
+
Class WebDriver{
|
11
|
+
|
12
|
+
[Diagnostics.Process] $Process
|
13
|
+
[UInt16] $LocalPort
|
14
|
+
|
15
|
+
WebDriver([String] $WebDriver){
|
16
|
+
$This.Process = start $WebDriver -WindowStyle Hidden -PassThru
|
17
|
+
do{
|
18
|
+
$This.LocalPort = Get-NetTCPConnection -OwningProcess $This.Process.Id -LocalAddress "127.0.0.1" -State Listen -ErrorAction SilentlyContinue|% LocalPort
|
19
|
+
}while($This.LocalPort -eq 0)
|
20
|
+
|
21
|
+
}
|
22
|
+
|
23
|
+
[PsCustomObject] Get([String] $Url){
|
24
|
+
$rst = Invoke-RestMethod -Method Get -uri "http://localhost:$($This.LocalPort)$Url"
|
25
|
+
return $rst.value
|
26
|
+
}
|
27
|
+
[PsCustomObject] Post([String] $Url, [PsCustomObject] $Arg){
|
28
|
+
$Json = $Arg|ConvertTo-Json -Compress
|
29
|
+
$Body = [Text.Encoding]::UTF8.GetBytes($Json)
|
30
|
+
$rst = Invoke-RestMethod -Method Post -uri "http://localhost:$($This.LocalPort)$Url" -body $Body -ContentType Application/json
|
31
|
+
return $rst.value
|
32
|
+
}
|
33
|
+
[WebSession] NewSession(){
|
34
|
+
$Body = @{capabilities=@{}}
|
35
|
+
$Result = $This.Post("/session",$Body)
|
36
|
+
return [WebSession]::new($This, $Result.SessionID)
|
37
|
+
}
|
38
|
+
}
|
39
|
+
|
40
|
+
Class WebSession{
|
41
|
+
|
42
|
+
[WebDriver] $WebDriver
|
43
|
+
[String] $SessionID
|
44
|
+
|
45
|
+
WebSession([WebDriver] $WebDriver, [String] $SessionID){
|
46
|
+
$This.WebDriver = $WebDriver
|
47
|
+
$This.SessionID = $SessionID
|
48
|
+
}
|
49
|
+
[PsCustomObject] NavigateTo([String] $Url){
|
50
|
+
$body = @{url=$Url}
|
51
|
+
return $This.WebDriver.Post("/session/$($This.SessionID)/url",$body)
|
52
|
+
}
|
53
|
+
[WebElement] GetActiveElement(){
|
54
|
+
$Result = $This.WebDriver.Get("/session/$($This.SessionID)/element/active")
|
55
|
+
return [WebElement]::New($This, $Result."element-6066-11e4-a52e-4f735466cecf")
|
56
|
+
}
|
57
|
+
}
|
58
|
+
|
59
|
+
Class WebElement{
|
60
|
+
|
61
|
+
[WebDriver] $WebDriver
|
62
|
+
[WebSession] $WebSession
|
63
|
+
[String] $ElementID
|
64
|
+
|
65
|
+
WebElement([WebSession] $WebSession, [String] $ElementID){
|
66
|
+
$This.WebDriver = $WebSession.WebDriver
|
67
|
+
$This.WebSession = $WebSession
|
68
|
+
$This.ElementID = $ElementID
|
69
|
+
}
|
70
|
+
}
|
71
|
+
|
72
|
+
|
73
|
+
$EdgeDriver = [WebDriver]::new("D:\Items\Programing\PowerShell\ps\tool\WebDriver\msedgedriver.exe")
|
74
|
+
$sess = $Edge.NewSession()
|
75
|
+
$sess.NavigateTo("https://teratail.com/")
|
76
|
+
$Element = $sess.GetActiveElement()
|
77
|
+
|
78
|
+
# これがHTML上の何処に存在しているのか探したい
|
79
|
+
$Element.ElementID
|
80
|
+
|
81
|
+
$EdgeDriver.Process.Kill()
|
82
|
+
```
|