0

rSource is set to the value of cell A1, which contains a number.

The next part of my code opens up a website and enters rSource into field 'num' which is just a input box. The button is then clicked, which causes the website to calculate the square root of the number entered before displaying it in the box 'answer'.

The problem with my code seems to be this line:

variableName = driver.findElementByName("answer").getAttribute("value")

What I want is to grab the attribute of 'answer' which will be the square root of the number I entered earlier, and then paste it into cell A2.

I've noticed for one thing that getAttribute doesn't seem to be the same as 'copying', as when I try to manually paste into a cell I never get 'answer'. What I was hoping to do was somehow 'copy' that value, and then use something like ' Worksheets("Sheet1").Range("A2").PasteSpecial' to then paste 'answer' into cell A2... Though after a ton of playing around I can't seem to get it to work. Any help would be much appreciated.

The above code was created using Selenium, which I then modified using VBA.

3
  • Have you tried highlighting the text and using selenium SendKeys for this? Highlighting is possible with Selenium. On the other hand, if you really need it to come from the clipboard, the Clipboard object is a good alternative. Commented Nov 14, 2013 at 11:09
  • I can't remember what I did before, but it was something along the lines of: driver.findElementByName("answer").SendKeys (Keys.Control + "C") which wasn't working for me Commented Nov 14, 2013 at 11:16
  • Please see my answer below. It's not exactly Selenium-style but it can possibly help. Commented Nov 14, 2013 at 11:22

2 Answers 2

3

As I said in the comments, Selenium can use SendKeys for this. However, if you should really need the copy-paste action, I suggest using the Clipboard instead.

Please note this needs the Microsoft Forms 2.0 Object Library to be checked in your references. If this is not appearing, add a UserForm to your file, and it will appear as checked by default.

The following subroutine will put a string of text into your clipboard. Executing Ctrl-V after running this will emulate a pasting action.

Sub Boom()
Dim DataObj As MSForms.DataObject
Set DataObj = New MSForms.DataObject
With DataObj
    .SetText "Answer"
    .PutInClipboard
End With
End Sub

Just replace "Answer" with whatever value you want. Better yet, modify the above to be able to take in text strings like below:

Sub Boom(Str As String)
Dim DataObj As MSForms.DataObject
Set DataObj = New MSForms.DataObject
With DataObj
    .SetText Str
    .PutInClipboard
End With
End Sub

Now you can do Boom("Answer") on it and it will put "Answer" to the Clipboard. You can then paste it using Range("A1").PasteSpecial xlPasteAll or whatever.

Hope this can help you out. Not really the Selenium path but at least it's a very clean option. :)

EDIT: To use Selenium's SendKeys, it goes something like:

Sub CopyTest()

Dim Sel As New SeleniumWrapper.WebDriver
Dim keys As New SeleniumWrapper.keys

Sel.SendKeys keys.Control & "c"

End Sub

Hope that helps. :)

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the response, could you give me an example of using SendKeys to simulate pressing CTRL + C for example? Every time I use it I get errors but it's possible my syntax is wrong. Oh and just trying your solution now.
Check my edit above. Note that I'm using & where you used +. & is the correct one.
Worked like a charm, thanks a lot! I'm not sure why but SendKeys just seems to simply hate me. In the end I used your clipboard method which worked perfectly.
0

There is a simple fix. Do not use copy-paste. replace .getattribute (value) with .text:

variableName = driver.findElementByName("answer").Text

FYI, For multiple elements I use .GetData like so:

arrayName = driver.findElementsByClassName("answer").GetData

hth

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.