1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

python+selenium execute_scriptを使うと半角カタカナが文字化けしない

Posted at

検証環境

  • Windows 10 Pro, 64bit Version 2004 ビルド 19041
  • Python 3.8.4
  • ChromeDriver 83.0.4103.39 (ccbf011cb2d2b19b506d844400483861342c20cd-refs/branch-heads/4103@{#416})
  • selenium.version 3.141.0

send_keys、execute_scriptをそれぞれ使ったサンプル

【Selenium】SendKeysの文字入力が遅いときは『executeScript』で入力すると一瞬 | シラベルノートを参考にしました。

send_keysをつかう(文字化けするパターン)
el = driver.find_element_by_id('target_id')
el.send_keys('モジバケ シマス')
execute_scriptをつかう(文字化け回避できる)
driver.execute_script(
  'document.getElementById("target_id").value="%s";' %
  'モジバケ シナイ!'
)

半角カタカナが文字化けする理由について見つかった記事

深堀りして調べておりませんが、IEのWebDriverはパッチを当てられると書いてありますね。ChromeDriverはレポジトリが公開されていなさそう。

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?