-1

I'm new to selenium, & not familiar with its syntax.

I'm trying to figure out how to send Control A & save all the selected text to a file.

If someone could post some examples of sending Control A & saving all the selected text to a basic text file, it'd save me huge amounts of time.

I've managed to get selenium working, but cant figure out the syntax as its different from Vba ...

Thanks.,

Public Sub scrapeCIL()
Dim bot As New WebDriver, posts As WebElements, post As WebElement, i As Integer, mysheet As Worksheet, keys As Selenium.keys
bot.Start "chrome", "https://www.binance.com/en/trade/BTC_USDT"
bot.Get "/"


bot.Quit
End Sub

1 Answers1

1

you can do CTRL + A using the below code :

wd.findElementByTagName("body").click
wd.SendKeys Keys.Control, "a"

and create your text file and write it like this :

Option Explicit

' Go to Tools -> References... and check "Microsoft Scripting Runtime" to be able to use
' the FileSystemObject which has many useful features for handling files and folders
Public Sub SaveTextToFile()

    Dim filePath As String
    filePath = "C:\temp\MyTestFile.txt"

    ' The advantage of correctly typing fso as FileSystemObject is to make autocompletion
    ' (Intellisense) work, which helps you avoid typos and lets you discover other useful
    ' methods of the FileSystemObject
    Dim fso As FileSystemObject
    Set fso = New FileSystemObject
    Dim fileStream As TextStream

    ' Here the actual file is created and opened for write access
    Set fileStream = fso.CreateTextFile(filePath)

    ' Write something to the file
    fileStream.WriteLine "something"

    ' Close it, so it is not locked anymore
    fileStream.Close

    ' Here is another great method of the FileSystemObject that checks if a file exists
    If fso.FileExists(filePath) Then
        MsgBox "Yay! The file was created! :D"
    End If

    ' Explicitly setting objects to Nothing should not be necessary in most cases, but if
    ' you're writing macros for Microsoft Access, you may want to uncomment the following
    ' two lines (see https://stackoverflow.com/a/517202/2822719 for details):
    'Set fileStream = Nothing
    'Set fso = Nothing

End Sub

Reference taken from How to create and write to a txt file using VBA

cruisepandey
  • 28,520
  • 6
  • 20
  • 38
  • Hi, Thanks for the reply I shouldve said save the selected text using control a, in selenium directly from the browser, not vba I know how to use vba to save files, but i dont know how to save text using selenium directly from the browser Thanks! –  May 26 '21 at 11:16
  • @GenusMax : The copied text is in universal clipboard, Can you read from from a clipboard ? – cruisepandey May 26 '21 at 14:02
  • Hi I tried bot.findElementByTagName("body").Click bot.SendKeys keys.Control, "a" But it gives Object does not support this method or property error. I just need to save all the text from https://www.binance.com/en/trade/BTC_USDT every 1 second & save it to a file. Doesnt have to use Ctrl A. The text has to be saved to a variable, not clipboard. I cant use the clipboard as i use the clipboard in another script. Thanks! –  May 26 '21 at 15:42