JimmyK
JimmyK

Reputation: 4992

findElement By XPath not supported in Selenium VBA?

variableName = driver.findElement(By.XPath(".//*[@id='T_F2']/fieldset/div[1]/div/div[4]/span[2]"))

Running the above always seems to lead to the error:

enter image description here

Why is this? I always see other people using findElement By XPath. If it helps, I generated about half of my code using Selenium's 'record' feature. I then converted the code into 'VBA/Webdriver' before pasting it into Excel to use as a Macro.

What exactly is wrong with my code? I have used findElement a number of times before, so I'd have to guess that the problem is with the By.XPath part of my code... Is there any way around this?

Edit: Even variableName = driver.findElementsByXPath(".//*[@id='T_F2']/fieldset/div[1]/div/div[4]/span[2]") leads to the error 'Invalid procedure call or argument' even though it looks fine to me.

Upvotes: 0

Views: 15603

Answers (3)

KevinS
KevinS

Reputation: 21

(I know it's been more than 6 years, but it's my first contribution and I can't resist! Maybe that can help someone else)

You have to initialise the following:

Dim By As New By, variableName As WebElement

and because variableName is an object, it has to be declared, as follows:

Set variableName = driver.FindElement(By.Xpath(".//*[@id='T_F2']/fieldset/div[1]/div/div[4]/span[2]"))

Upvotes: 0

jlookup
jlookup

Reputation: 66

Here's what works for me:

Dim variableName() as variant

variableName = driver.findElementsByXPath("//div[@id='T_F2']/fieldset/div[1]/div/div[4]/span[2]").getdata

Notice it's "find elements [plural] by XPath". This creates a two-dimensional array. variableName(1,1) will have the data you're looking for.

Upvotes: 0

WGS
WGS

Reputation: 14169

Try:

variableName = driver.findElementByXPath("//div[@id='T_F2']/fieldset/div[1]/div/div[4]/span[2]")

Notice that I removed the . in the beginning of the xPath and replaced * with div. Also, you're missing something at the end. You are just declaring the path here and not really getting a value.

EDIT: Referring to just the xPath is not usually enough. Do you want to perform an action on it, get the text inside, the tagname, etc.?

EDIT2: Testing to get the .Text attribute returns a "findElement By XPath not supported in Selenium VBA?" message.

Upvotes: 4

Related Questions