回答編集履歴

1

追記

2020/11/01 21:34

投稿

Reach
Reach

スコア733

test CHANGED
@@ -119,3 +119,155 @@
119
119
  driver.quit()
120
120
 
121
121
  ```
122
+
123
+
124
+
125
+ 上記コードは ウインドウサイズ変わるので
126
+
127
+ ```Python
128
+
129
+ import time
130
+
131
+ from selenium import webdriver
132
+
133
+ from selenium.webdriver.firefox.options import Options
134
+
135
+ from selenium.webdriver.common.keys import Keys
136
+
137
+
138
+
139
+ from ctypes import *
140
+
141
+ import ctypes
142
+
143
+ import pyautogui
144
+
145
+
146
+
147
+
148
+
149
+ # ログイン保持でFirefoxを起動するためのプロファイル設定
150
+
151
+ fp = webdriver.FirefoxProfile('C:\Users\motoy\AppData\Roaming\Mozilla\Firefox\Profiles\010hmdn9.default-release')
152
+
153
+ driver = webdriver.Firefox(fp)
154
+
155
+
156
+
157
+
158
+
159
+ # ログイン保持ではなくゲストとして起動すると問題なくタブが開けます
160
+
161
+ #driver = webdriver.Firefox()
162
+
163
+
164
+
165
+
166
+
167
+ # 検索するワード
168
+
169
+ key_words = 'モロッカンオイル 50ml'
170
+
171
+
172
+
173
+
174
+
175
+ # 最初のタブでGoogle検索
176
+
177
+ driver.get('https://www.google.com/')
178
+
179
+ time.sleep(1)
180
+
181
+ search_box = driver.find_element_by_name("q")
182
+
183
+ search_box.send_keys(key_words)
184
+
185
+ search_box.submit()
186
+
187
+ time.sleep(1)
188
+
189
+
190
+
191
+ #####################################################################
192
+
193
+ # 同じウィンドウ内の別タブで開く予定ですが、新規のウィンドウで開いてしまいます...
194
+
195
+ # 2番目のタブでヤフオクを開いてワードを検索する
196
+
197
+
198
+
199
+ EnumWindowsProc = CFUNCTYPE(c_bool, POINTER(c_int), POINTER(c_int))
200
+
201
+ def getWindowText(hwnd):
202
+
203
+ length = ctypes.windll.user32.GetWindowTextLengthW(hwnd) + 1
204
+
205
+ s = (ctypes.c_wchar * length)()
206
+
207
+ ctypes.windll.user32.GetWindowTextW(hwnd, ctypes.byref(s), length)
208
+
209
+ return s.value
210
+
211
+
212
+
213
+ def forground( hwnd, _):
214
+
215
+ name = getWindowText(hwnd)
216
+
217
+ if name.find('Mozilla Firefox') >= 0:
218
+
219
+ ctypes.windll.user32.SetForegroundWindow(hwnd)
220
+
221
+ return False
222
+
223
+
224
+
225
+ ctypes.windll.user32.EnumWindows( EnumWindowsProc(forground), None)
226
+
227
+ time.sleep(1)
228
+
229
+ pyautogui.hotkey('ctrl','t')
230
+
231
+ time.sleep(1)
232
+
233
+ #####################################################################
234
+
235
+
236
+
237
+
238
+
239
+ # 新しいタブに切り替える
240
+
241
+ driver.switch_to.window(driver.window_handles[1])
242
+
243
+ driver.get('https://auctions.yahoo.co.jp/')
244
+
245
+ time.sleep(4)
246
+
247
+
248
+
249
+
250
+
251
+ # ポップアップ広告が表示された場合の対応(広告を閉じる)
252
+
253
+ if len(driver.find_elements_by_xpath('/html/body/div[1]/div/section/div/a[2]')) > 0:
254
+
255
+ driver.find_element_by_xpath('/html/body/div[1]/div/section/div/a[2]').click()
256
+
257
+
258
+
259
+ # ヤフオクで検索
260
+
261
+ search_box = driver.find_element_by_name("p")
262
+
263
+ search_box.send_keys(key_words)
264
+
265
+ search_box.submit()
266
+
267
+ time.sleep(5)
268
+
269
+
270
+
271
+ driver.quit()
272
+
273
+ ```