質問編集履歴

1

反映されていなかった部分を追加

2021/07/27 13:35

投稿

kamikazelight
kamikazelight

スコア305

test CHANGED
File without changes
test CHANGED
@@ -1,9 +1,163 @@
1
1
  msedgedriverを使用してGetActiveElementなどでElementIDを取得した際に
2
2
 
3
- それがHTML上のどの要素なのかを特定shです。
3
+ それがHTML上のどの要素なのかを特定したいです。
4
4
 
5
5
 
6
6
 
7
7
  EdgeのデベロッパツールでIDを検索かけてみたりしたのですが
8
8
 
9
9
  見つけられません
10
+
11
+
12
+
13
+ 方法はありませんか?
14
+
15
+
16
+
17
+ ```PowerShell
18
+
19
+ Class WebDriver{
20
+
21
+
22
+
23
+ [Diagnostics.Process] $Process
24
+
25
+ [UInt16] $LocalPort
26
+
27
+
28
+
29
+ WebDriver([String] $WebDriver){
30
+
31
+ $This.Process = start $WebDriver -WindowStyle Hidden -PassThru
32
+
33
+ do{
34
+
35
+ $This.LocalPort = Get-NetTCPConnection -OwningProcess $This.Process.Id -LocalAddress "127.0.0.1" -State Listen -ErrorAction SilentlyContinue|% LocalPort
36
+
37
+ }while($This.LocalPort -eq 0)
38
+
39
+
40
+
41
+ }
42
+
43
+
44
+
45
+ [PsCustomObject] Get([String] $Url){
46
+
47
+ $rst = Invoke-RestMethod -Method Get -uri "http://localhost:$($This.LocalPort)$Url"
48
+
49
+ return $rst.value
50
+
51
+ }
52
+
53
+ [PsCustomObject] Post([String] $Url, [PsCustomObject] $Arg){
54
+
55
+ $Json = $Arg|ConvertTo-Json -Compress
56
+
57
+ $Body = [Text.Encoding]::UTF8.GetBytes($Json)
58
+
59
+ $rst = Invoke-RestMethod -Method Post -uri "http://localhost:$($This.LocalPort)$Url" -body $Body -ContentType Application/json
60
+
61
+ return $rst.value
62
+
63
+ }
64
+
65
+ [WebSession] NewSession(){
66
+
67
+ $Body = @{capabilities=@{}}
68
+
69
+ $Result = $This.Post("/session",$Body)
70
+
71
+ return [WebSession]::new($This, $Result.SessionID)
72
+
73
+ }
74
+
75
+ }
76
+
77
+
78
+
79
+ Class WebSession{
80
+
81
+
82
+
83
+ [WebDriver] $WebDriver
84
+
85
+ [String] $SessionID
86
+
87
+
88
+
89
+ WebSession([WebDriver] $WebDriver, [String] $SessionID){
90
+
91
+ $This.WebDriver = $WebDriver
92
+
93
+ $This.SessionID = $SessionID
94
+
95
+ }
96
+
97
+ [PsCustomObject] NavigateTo([String] $Url){
98
+
99
+ $body = @{url=$Url}
100
+
101
+ return $This.WebDriver.Post("/session/$($This.SessionID)/url",$body)
102
+
103
+ }
104
+
105
+ [WebElement] GetActiveElement(){
106
+
107
+ $Result = $This.WebDriver.Get("/session/$($This.SessionID)/element/active")
108
+
109
+ return [WebElement]::New($This, $Result."element-6066-11e4-a52e-4f735466cecf")
110
+
111
+ }
112
+
113
+ }
114
+
115
+
116
+
117
+ Class WebElement{
118
+
119
+
120
+
121
+ [WebDriver] $WebDriver
122
+
123
+ [WebSession] $WebSession
124
+
125
+ [String] $ElementID
126
+
127
+
128
+
129
+ WebElement([WebSession] $WebSession, [String] $ElementID){
130
+
131
+ $This.WebDriver = $WebSession.WebDriver
132
+
133
+ $This.WebSession = $WebSession
134
+
135
+ $This.ElementID = $ElementID
136
+
137
+ }
138
+
139
+ }
140
+
141
+
142
+
143
+
144
+
145
+ $EdgeDriver = [WebDriver]::new("D:\Items\Programing\PowerShell\ps\tool\WebDriver\msedgedriver.exe")
146
+
147
+ $sess = $Edge.NewSession()
148
+
149
+ $sess.NavigateTo("https://teratail.com/")
150
+
151
+ $Element = $sess.GetActiveElement()
152
+
153
+
154
+
155
+ # これがHTML上の何処に存在しているのか探したい
156
+
157
+ $Element.ElementID
158
+
159
+
160
+
161
+ $EdgeDriver.Process.Kill()
162
+
163
+ ```