[vba][vba seleniuim]利用sendKeys清除input內的值並填入新文字

  • 979
  • 0

2019/06/15

vba selenium內建的clear功能會報錯,網路上有高手提出解決方案,就是用sendKeys的方式去將input內文字以模擬ctrl+A的方式全選後用模擬按下delete的方式刪除

https://stackoverflow.com/questions/7732125/clear-text-from-textarea-with-selenium

...

 

以下是範例程式碼:

Public BOT As New WebDriver
Sub test()

'連上網址
BOT.Start "chrome", "網址"
BOT.Get "/"

txt ="test test test" '測試文字

BOT.FindElementById("id").SendKeys BOT.keys.Enter '先模擬enter動作,也就是選取該input元素
BOT.FindElementById("id").SendKeys BOT.keys.Control + "a" '全選input內所有文字
BOT.FindElementById("id").SendKeys BOT.keys.Delete  '刪除
BOT.FindElementById("id").SendKeys txt '在input內填入測試文字

end sub