ポイント:
- Edgeブラウザでは、`webdriver.EdgeOptions`を使用する必要があります。
- 変更箇所:
```python
from selenium.webdriver.edge.options import Options
edge_options = Options()
```
---------------------------------------------------------
import time
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.microsoft import EdgeChromiumDriverManager
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
class KakakuTdMemoryPrintTest :
def print_single_quantity(self) :
# Selenium WebDriver の設定
chrome_options = Options()
# ブラウザをヘッドレスモードで起動(任意)
chrome_options.add_argument("--headless")
# service = Service('path_to_chromedriver') # chromedriver のパスを指定
# driver = webdriver.Chrome(service=service, options=chrome_options)
service = Service(EdgeChromiumDriverManager().install())
driver = webdriver.Edge(service=service,options=chrome_options )
# 対象のページを開く
# ターゲットのURLを指定
#url = "https://example.com/your-target-page"
#価格COM ノートパソコン # ターゲットのURLを指定
url = "https://kakaku.com/pc/note-pc/itemlist.aspx"
driver.get(url)
time.sleep(1)
# `class="td-price"` を含む `tr` 要素のリストを取得
tr_elements = driver.find_elements( By.XPATH, "//tr[td[contains(@class, 'td-price')]]")
time.sleep(1)
# 各 `tr` 要素の情報を取得・表示
tr = tr_elements[0]
#ひとつのtrに含まれる すべてのtdをリストとして取得
list_td = driver.execute_script( "return arguments[0].childNodes;", tr )
td_memory_quantity = list_td[ 10 ]
# td_memory_quantity というのは dict型らしい。
#a_memory_quantity = td_memory_quantity.find_element( By.XPATH, ".//span/a" )
#text_memory_quantity = a_memory_quantity.text
#print( text_memory_quantity )
print( td_memory_quantity )
kakaku_td10B = KakakuTdMemoryPrintTest()
kakaku_td10B.print_single_quantity()
上記の selenium with pythonを使ったコードを実行してみると、
以下のようなエラーが表示されました。
Traceback (most recent call last):
File "C:\03_w\eclipse_workspace_202412\scraping_test_202501\kakaku_com_check01\td_memory_z_print.py", line 82, in <module>
kakaku_td10B.print_single_quantity()
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
File "C:\03_w\eclipse_workspace_202412\scraping_test_202501\kakaku_com_check01\td_memory_z_print.py", line 39, in print_single_quantity
driver = webdriver.Edge(service=service,options=chrome_options )
File "C:\Users\user\AppData\Local\Programs\Python\Python313\Lib\site-packages\selenium\webdriver\edge\webdriver.py", line 45, in __init__
super().__init__(
~~~~~~~~~~~~~~~~^
browser_name=DesiredCapabilities.EDGE["browserName"],
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...<3 lines>...
keep_alive=keep_alive,
^^^^^^^^^^^^^^^^^^^^^^
)
^
File "C:\Users\user\AppData\Local\Programs\Python\Python313\Lib\site-packages\selenium\webdriver\chromium\webdriver.py", line 66, in __init__
super().__init__(command_executor=executor, options=options)
~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\user\AppData\Local\Programs\Python\Python313\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 241, in __init__
self.start_session(capabilities)
~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^
File "C:\Users\user\AppData\Local\Programs\Python\Python313\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 329, in start_session
response = self.execute(Command.NEW_SESSION, caps)["value"]
~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\user\AppData\Local\Programs\Python\Python313\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 384, in execute
self.error_handler.check_response(response)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^
File "C:\Users\user\AppData\Local\Programs\Python\Python313\Lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 232, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.SessionNotCreatedException: Message: session not created: No matching capabilities found
Stacktrace:
(No symbol) [0x00007FF6B2D56B05]
Microsoft::Applications::Events::EventProperty::empty [0x00007FF6B307F464+1437348]
sqlite3_dbdata_init [0x00007FF6B3122D96+643174]
(No symbol) [0x00007FF6B2CA444E]
(No symbol) [0x00007FF6B2CA35E0]
(No symbol) [0x00007FF6B2CA51C4]
(No symbol) [0x00007FF6B2C9BE03]
(No symbol) [0x00007FF6B2C72984]
(No symbol) [0x00007FF6B2C71E30]
(No symbol) [0x00007FF6B2C72571]
Microsoft::Applications::Events::EventProperty::empty [0x00007FF6B302BAF4+1094964]
(No symbol) [0x00007FF6B2D932B8]
Microsoft::Applications::Events::EventProperty::empty [0x00007FF6B302AF33+1091955]
Microsoft::Applications::Events::EventProperty::empty [0x00007FF6B302AA99+1090777]
Microsoft::Applications::Events::ILogConfiguration::operator* [0x00007FF6B2E30CA1+461569]
Microsoft::Applications::Events::ILogConfiguration::operator* [0x00007FF6B2E2C9C4+444452]
Microsoft::Applications::Events::ILogConfiguration::operator* [0x00007FF6B2E2CB09+444777]
Microsoft::Applications::Events::ILogConfiguration::operator* [0x00007FF6B2E22186+401382]
BaseThreadInitThunk [0x00007FFA40A4E8D7+23]
RtlUserThreadStart [0x00007FFA417BFBCC+44]
これは ブラウザが chromeではなく、 edgeだと
optionの指定のしかたが異なるからでしょうか?
エラーの原因は、Edgeブラウザで`Options`の指定方法がChromeとは異なるためです。`webdriver.Edge`では、`Options`を`webdriver.EdgeOptions`として設定する必要があります。以下の修正を試してください。
### 修正版コード
以下に修正版を示します:
```python
import time
from selenium import webdriver
from webdriver_manager.microsoft import EdgeChromiumDriverManager
from selenium.webdriver.edge.service import Service
from selenium.webdriver.edge.options import Options
from selenium.webdriver.common.by import By
class KakakuTdMemoryPrintTest:
def print_single_quantity(self):
# Selenium WebDriver の設定
edge_options = Options()
# ブラウザをヘッドレスモードで起動(任意)
edge_options.add_argument("--headless")
# WebDriverを初期化
service = Service(EdgeChromiumDriverManager().install())
driver = webdriver.Edge(service=service, options=edge_options)
try:
# 対象のページを開く
url = "https://kakaku.com/pc/note-pc/itemlist.aspx"
driver.get(url)
time.sleep(1)
# `class="td-price"` を含む `tr` 要素のリストを取得
tr_elements = driver.find_elements(By.XPATH, "//tr[td[contains(@class, 'td-price')]]")
time.sleep(1)
# 各 `tr` 要素の情報を取得・表示
if tr_elements:
tr = tr_elements[0]
# ひとつのtrに含まれるすべてのtdをリストとして取得
list_td = driver.execute_script("return arguments[0].childNodes;", tr)
if len(list_td) > 10:
td_memory_quantity = list_td[10]
# 出力
print(td_memory_quantity)
else:
print("td要素が不足しています。")
else:
print("tr要素が見つかりませんでした。")
finally:
driver.quit()
# 実行
kakaku_td10B = KakakuTdMemoryPrintTest()
kakaku_td10B.print_single_quantity()
```
### 修正内容のポイント
1. **`Options`の使用**:
- Edgeブラウザでは、`webdriver.EdgeOptions`を使用する必要があります。
- 変更箇所:
```python
from selenium.webdriver.edge.options import Options
edge_options = Options()
```
2. **WebDriverの初期化**:
- `webdriver.Edge`に渡す`options`は、Edge用に設定されたものを指定する必要があります。
3. **例外処理とブラウザ終了**:
- `try-finally`を使用して、スクリプト終了時に必ずブラウザを終了するようにしました。
### その他の注意点
- `childNodes`で取得した`td`が正しい要素か確認してください。必要に応じてXPathでさらに絞り込む方が堅牢なコードになります。
- ヘッドレスモードで動作しない場合は、ヘッドレスモードをコメントアウトして動作を確認してください。
この修正版で動作するはずです。再度試してみてください!
コメント
コメントを投稿