VineGlobal / selenium-vba

Automatically exported from code.google.com/p/selenium-vba
1 stars 0 forks source link

WebDriver.switchToFrame Method returns "Specified cast is not valid" error #92

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Operating system and version (32/64bit) :  Windows 7 64 bit
.Net Framework version :  4.5.50709
Office name and version(32/64bit) :  14.7015.1000 (32 bit)
Browser name and version :ie, chrome, firefox, safari
SeleniumWrapper version :  1.0.19.0

The issue, using the integer index input for the switchToFrame() Method returns 
an error.
The application I am working on does not have a name or ID for the frames, 
unlike the sample page provided in demo. 

What steps will reproduce the problem with a public website ?
See the two Excel functions below to reproduce the issue.  See screenshot 
attached.
Function DemoWorks uses the name attribute as a string to switch to the frame.
Function DemoFails uses the index as an integer to switch to the frame.

From the documentation provided in the SeleniumWrapper API Documentation under 
WebDriver.switchToFrame Method, the input type is identified as both an integer 
and an object.
"index_or_name As Object" this states an object is required for the input 
"The name of the window to switch to, or an integer representing the index to 
switch to." this states the index requires an integer.
Please confirm which type is correct.

What is the expected output? 
using an integer index in switchToFrame(1) successfully changes the webdriver 
to the dom of the indexed frame.

What do you see instead?
An error is displayed "Specified cast is not valid" when executing line 
objWebDriver.switchToFrame (1) from function below.

Please provide any additional information below.

Function DemoWorks()
    Dim objWebDriver As New WebDriver
    Dim objWebElementCollection As WebElementCollection
    objWebDriver.Start "ie", "http://www.quackit.com/html/templates/frames/frames_example_1.html"
    objWebDriver.windowMaximize
    objWebDriver.Open "http://www.quackit.com/html/templates/frames/frames_example_1.html"
    objWebDriver.switchToFrame ("content")
    Set objWebElementCollection = objWebDriver.findElementsByTagName("a")
    objWebElementCollection.Item(0).Click
    Set objWebElementCollection = Nothing
    Set objWebElementCollection = objWebDriver.findElementsByTagName("a")
    objWebElementCollection.Item(0).Click
    Set objWebElementCollection = Nothing
End Function

I discovered two ways to cast the WebDriver variable, both return same error. 
(Is either approach preferred?  What is the difference?)
Also, I tried to pass the index as a WebElement frame object which failed and 
passing the index number as a string failed.

Function DemoFails()
    Dim objWebDriver As New WebDriver
    '    Dim objWebDriver As New SeleniumWrapper.WebDriver
    Dim objWebElementCollection As WebElementCollection
    objWebDriver.Start "ie", "http://www.quackit.com/html/templates/frames/frames_example_1.html"
    objWebDriver.windowMaximize
    objWebDriver.Open "http://www.quackit.com/html/templates/frames/frames_example_1.html"
    objWebDriver.switchToFrame (1)
    Set objWebElementCollection = objWebDriver.findElementsByTagName("a")
    objWebElementCollection.Item(0).Click
    Set objWebElementCollection = Nothing
End Function

Thank you.

Original issue reported on code.google.com by jospe...@gmail.com on 6 Sep 2014 at 2:58

Attachments:

GoogleCodeExporter commented 9 years ago
In VBA/VBS primitives are straightforward to assign to a variable:
 Dim v1: v1 = 23
 Dim v2: v2 = "text"
 Dim v3: v3 = function_retrun_primitive()
Whereas objects require the Set keyword in front:
 Dim v4: Set v4 = object
 Dim v5: Set v5 = function_return_object()

The reason the switchToFrame is failing could be that you are providing the 
wrong index.
Moreover, in VBA/VBA a function that doesn't return anything shouldn't have 
parenthesis.
So a correct use in your case would be:
Function Demo()
    Dim wd As New WebDriver
    wd.Start "ie", "http://www.quackit.com"
    wd.windowMaximize
    wd.Open "/html/templates/frames/frames_example_1.html"
    'To select the first frame:
    wd.switchToFrame 0
    'To click the first link:
    wd.findElementByXPath("//a[0]").Click
    ' or:
    wd.findElementByCssSelector("a:nth-child(0)").Click
    ' or:
    wd.findElementsByTagName("a").Item(0).Click
    ' or:
    Dim eles As WebElementCollection
    Set eles = wd.findElementsByTagName("a")
    eles(0).Click
End Function

Original comment by florentbr on 8 Sep 2014 at 5:21

GoogleCodeExporter commented 9 years ago
Thanks for the response.
The modified function provided is still returning the same error, "Specified 
cast is not valid." 
There is no difference when attempting the following.
wd.switchToFrame 0

wd.switchToFrame (0)

dim intFrame as integer
intFrame = 0
wd.switchToFrame intFrame 

The frame is confirmed on the page and the index number is correct.

Any other suggestion?

Original comment by jospe...@gmail.com on 9 Sep 2014 at 5:57

GoogleCodeExporter commented 9 years ago
Looks like there is a bug with integers as index.
A workaround would be to provide a long instead (Ampersand at the end):
wd.switchToFrame 0&

Or to use the Selenium 1 command:
wd.selectFrame "index=0"

It should be fixed in the next release.

Original comment by florentbr on 9 Sep 2014 at 11:17

GoogleCodeExporter commented 9 years ago
Thank you for the work around.

The following worked successfully.
    objWebDriver.switchToFrame 1&

and
    Dim intIndex As Long
    intIndex = 1
    objWebDriver.switchToFrame intIndex

However, the Selenium 1 command did not.
    objWebDriver.selectFrame "index = 1"

I will use the Long Cast until the next release.

Thanks again.

Original comment by jospe...@gmail.com on 11 Sep 2014 at 3:12

GoogleCodeExporter commented 9 years ago
Fixed in v1.0.20.0

Original comment by florentbr on 18 Sep 2014 at 4:30