回答編集履歴
1
追記
answer
CHANGED
@@ -58,4 +58,80 @@
|
|
58
58
|
time.sleep(5)
|
59
59
|
|
60
60
|
driver.quit()
|
61
|
+
```
|
62
|
+
|
63
|
+
上記コードは ウインドウサイズ変わるので
|
64
|
+
```Python
|
65
|
+
import time
|
66
|
+
from selenium import webdriver
|
67
|
+
from selenium.webdriver.firefox.options import Options
|
68
|
+
from selenium.webdriver.common.keys import Keys
|
69
|
+
|
70
|
+
from ctypes import *
|
71
|
+
import ctypes
|
72
|
+
import pyautogui
|
73
|
+
|
74
|
+
|
75
|
+
# ログイン保持でFirefoxを起動するためのプロファイル設定
|
76
|
+
fp = webdriver.FirefoxProfile('C:\Users\motoy\AppData\Roaming\Mozilla\Firefox\Profiles\010hmdn9.default-release')
|
77
|
+
driver = webdriver.Firefox(fp)
|
78
|
+
|
79
|
+
|
80
|
+
# ログイン保持ではなくゲストとして起動すると問題なくタブが開けます
|
81
|
+
#driver = webdriver.Firefox()
|
82
|
+
|
83
|
+
|
84
|
+
# 検索するワード
|
85
|
+
key_words = 'モロッカンオイル 50ml'
|
86
|
+
|
87
|
+
|
88
|
+
# 最初のタブでGoogle検索
|
89
|
+
driver.get('https://www.google.com/')
|
90
|
+
time.sleep(1)
|
91
|
+
search_box = driver.find_element_by_name("q")
|
92
|
+
search_box.send_keys(key_words)
|
93
|
+
search_box.submit()
|
94
|
+
time.sleep(1)
|
95
|
+
|
96
|
+
#####################################################################
|
97
|
+
# 同じウィンドウ内の別タブで開く予定ですが、新規のウィンドウで開いてしまいます...
|
98
|
+
# 2番目のタブでヤフオクを開いてワードを検索する
|
99
|
+
|
100
|
+
EnumWindowsProc = CFUNCTYPE(c_bool, POINTER(c_int), POINTER(c_int))
|
101
|
+
def getWindowText(hwnd):
|
102
|
+
length = ctypes.windll.user32.GetWindowTextLengthW(hwnd) + 1
|
103
|
+
s = (ctypes.c_wchar * length)()
|
104
|
+
ctypes.windll.user32.GetWindowTextW(hwnd, ctypes.byref(s), length)
|
105
|
+
return s.value
|
106
|
+
|
107
|
+
def forground( hwnd, _):
|
108
|
+
name = getWindowText(hwnd)
|
109
|
+
if name.find('Mozilla Firefox') >= 0:
|
110
|
+
ctypes.windll.user32.SetForegroundWindow(hwnd)
|
111
|
+
return False
|
112
|
+
|
113
|
+
ctypes.windll.user32.EnumWindows( EnumWindowsProc(forground), None)
|
114
|
+
time.sleep(1)
|
115
|
+
pyautogui.hotkey('ctrl','t')
|
116
|
+
time.sleep(1)
|
117
|
+
#####################################################################
|
118
|
+
|
119
|
+
|
120
|
+
# 新しいタブに切り替える
|
121
|
+
driver.switch_to.window(driver.window_handles[1])
|
122
|
+
driver.get('https://auctions.yahoo.co.jp/')
|
123
|
+
time.sleep(4)
|
124
|
+
|
125
|
+
|
126
|
+
# ポップアップ広告が表示された場合の対応(広告を閉じる)
|
127
|
+
if len(driver.find_elements_by_xpath('/html/body/div[1]/div/section/div/a[2]')) > 0:
|
128
|
+
driver.find_element_by_xpath('/html/body/div[1]/div/section/div/a[2]').click()
|
129
|
+
|
130
|
+
# ヤフオクで検索
|
131
|
+
search_box = driver.find_element_by_name("p")
|
132
|
+
search_box.send_keys(key_words)
|
133
|
+
search_box.submit()
|
134
|
+
time.sleep(5)
|
135
|
+
|
136
|
+
driver.quit()
|
61
137
|
```
|