1
3

More than 3 years have passed since last update.

Selenium でテキストボックスに入力する vba python

Last updated at Posted at 2020-07-23

Selenium or seleniumbasicテキストボックスに文字列を入力する

前提

・vbaの場合は seleniumbasic pythonの場合はselenium がインストールされている
・環境はWIndows

ポイント

1, inputタグに入力する前に消す Clear ※これがないと追記されてしまう。
2, valueの値でSendKeysができてるか確認
3, 早すぎると入力できてるか不安と言われるので sleep や waitを使う(これは状況に応じて)

以上を考慮し、
inputタグ(テキストボックス)にidのある場合で関数を作成してみました。

VBAの場合

引渡値 = ①クロームドライバー ②inputタグのid ③inputタグに入力する値
戻り値 = 5秒以内に 成功=true 失敗=false

inputタグ(テキストボックス)に入力する
Public Function MoveTextBox(driver As ChromeDriver, id As String, atai As String) As Boolean            
   Dim timeout As Date
   timeout = DateAdd("s", 5, Now)       
   Do Until driver.FindElementById(id).Value = atai
     driver.FindElementById(id).Clear
     driver.Wait 100
     driver.FindElementById(id).SendKeys (atai)
    driver.Wait 100
    If Now > timeout Then
      MoveTextBox = False
      Exit Function
    End If              
   Loop
   MoveTextBox = True        
 End Function


Pythonの場合

引渡値 = ①クロームドライバー ②inputタグのid ③inputタグに入力する値
戻り値 = 5秒以内に 成功=true 失敗=false
sleepはなくてもよいのですが、あったほうが見ていてわかりやすいと評判がいいです。

inputタグ(テキストボックス)に入力する

import time
def move_textbox(driver, id, atai):
    start = time.time()
    while driver.find_element_by_id(id).get_attribute('value')!=atai:
        driver.find_element_by_id(id).clear()
        time.sleep(1)        
        driver.find_element_by_id(id).send_keys(atai)
        time.sleep(1)

        if time.time()-start > 5 :
            return False


    return True

関数の例はinputタグにidのある場合ですが、
nameの場合は適宜
FindElementByName
find_element_by_name
に変えれば使いまわしがきくと思います

1
3
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
3