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:
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.
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.
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.
(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]"))
Related
My question relates to coding selenium specifically in VBA.
I have a function that finds webelements based on text passed to it (in string variable 'toFind'). The relevent Xpath identification method I use is (where driver. is the selenium chromedriver):
mySearch = "//*[contains(text(),'" & toFind & "')]"
Set ret = driver.FindElementsByXPath(mySearch)
This works unless the toFind variable contains an apostrophe. For example if "Consultant's Forename" is passed then my expression evaluates to:
Set ret = driver.FindElementsByXPath("//*[contains(text(),'Consultant's Forename')]"), which causes an invalid selector run-time error.
I have researched elsewhere on the site and see a number of answers describing escaping from the single quotes using the backslash character. Based on this I have tried to use Set ret = driver.FindElementsByXPath("//*[contains(text(),\"Consultant's Forename\")]") instead. However, this will not compile in microsoft visual basic for applications as it reports a syntax error (code line is red). I have not tried using the driver.findElements(By.xpath method as opposed to driver.FindElementsByXPath as I assumed this would not make a difference to the handling of the XPath expression. I have tried the other suggestions of using the 'concat' function but this also seems not to be valid in VBA selenium.
I don't know if these methods are specficaly for platforms other than VBA or I am just getting my syntax wrong?
The only way I can work it at present is to ignore the existence of the apostrophe:
Set ret = driver.FindElementsByXPath("//*[contains(text(),'Consultant') and contains(text(),'s Forename')]")
Whilst this works it is an incomplete solution and any help on the correct syntax to deal with the xpath location in VBA for text containing an apostrophe would be much appreciated.
I just created this tiny Office script:
async function main(context: Excel.RequestContext) {
context.workbook.names.getItem("Newname").delete;
}
The range name 'Newname' exists in the current workbook, the code runs without error but the name is not deleted. Why? I would expect to receive a runtime error if I'm not allowed to delete range names.
I think it missed the bracket:
context.workbook.names.getItem("Newname").delete();
I just tried by this gist, you could have a try. https://gist.github.com/lumine2008/f55a6265a93b421112de210b22e9a48a
delete is a method. In JavaScript, a method requires opening and closing parentheses, even if there are no parameters to be passed:
context.workbook.names.getItem("Newname").delete();
My tests confirm this. Also, the code should include await.context.sync();
It's important to realize that JavaScript supports assigning a method to a variable (object), similar to assigning a function to an Excel Name, by leaving off the parentheses. This object can then be used at some point to execute the method. This is why no errors are thrown when the parentheses are omitted.
Example:
const deleteNewName = context.workbook.names.getItem("Newname").delete;
deleteNewName();
(Thanks to Lumpenstein for this information about JS kindly provided in a comment.)
I'm looking to automate a very basic xpath check from Excel using Selenium and am getting a run-time error '7': Out of memory.
I've succesfully automated quite a bit using AHK but new to VBA so I'm curious what might be causing the problem. Currently the page loads but as soon as it goes to check the XPath it almost immediately pops up with an out of memory run-time error.
I've double checked to see if there are extra processes running or if I'm low on memory and neither seems to be an issue. I have plenty of free memory and have closed all processes so I assume there is an error with my code or some issue I'm not aware of. I've updated to the newest version of ChromeDriver for Selenium and am pretty certain that the Xpath is correct.
Public Sub DupeCheck()
Dim CID As String
Dim HireURL As String
Dim SearchResult As String
CID = 14962738
HireURL = "https://hire.amazon.com/search?q=" & CID & "&sort=relevance_desc&type=candidate"
Dim bot As New WebDriver
bot.Start "chrome", HireURL
bot.Get "/"
SearchResult = bot.FindElementByXPath(".//*[#id='search-page-root']/div[1]/div[2]/div/div[2]/div/div/div[2]/div/div/ul/li[1]/div/div[1]/div[1]/div[2]").Attribute("innerText")
MsgBox (SearchResult)
bot.Quit
End Sub
Does anyone have any idea what might be causing the issue? I genuinely appreciate any help or ideas! Thanks!
For the html you have shown, and following our discussion, use a css attribute = value selector with ends with ($) operator to specify that the class attribute value ends with a specified substring
FindElementByCss("[class$='e15zxa0z0']")
If you need attribute values you can use the .Attribute method e.g.
FindElementByCss("[class$='e15zxa0z0']").Attribute("attributename")
XPath can be particularly brittle and css selectors are generally faster methods for selecting as modern browsers are optimized for css. They should also be less memory consuming.
Is this the only instance of an XPath you are using in your code? See memory exhaustion
I am using the following code to use the for loop with a list (I am familiar with the other syntax copied below as well). The first for loop is not working for me even though its the same list. Any ideas on how to go about troubleshooting this:
#{list items:0..items.size(), as:'item'}${item}#{/list} //Not Working - I have tried moving the items.size out of the loop as well but it has not worked for me
This is working for the same list:
#{list items, as:'item'}${item}#{/list}
Any thoughts/suggestions why? Thanks in advance.
Edit: The list is that of user defined instances (primarily holding Strings and Integers)
The syntax is incorrect, the items parameter need an iterable and you give it an integer, if you need an index you can use the item_index variable
I'm using a function in vbscript which returns a variant array of strings.
JobIDs = objDoc.ConnectedSubmit(objServer)
The problem is I can't get the Job ID values from that array, as vbscript doesn't handle typed variables. It just gives a type mismatch when I attempt to do ANYTHING with the JobIDs array. I found some promising information here, but when I used the conversion function:
Set objConverter = CreateObject("ADS.ArrayConvert")
ConvertedJobIDs = objConverter.CStrArray(JobIDs())
It is giving me the same type mismatch error. Am I missing something obvious here? This is, apparently, an official microsoft solution, so I'm not sure why it seems to have the same problem, namely, not being able to actually do anything with a string array in the first place. I've seen the first part of my question answered in many places, all pointing to the MS solution, but I have yet to see any follow up reports of someone successfully using that solution.
I'm not sure if I understand why it doesn't work, so this answer might not be very helpful. I would have thought that something like this might work (following on from your previous question I'm assuming you're trying to get the cancellation to work):
For Each id In JobIDs
WScript.Echo id
YourJob = YourOutgoingFaxQueue.GetJob(id)
YourJob.Cancel()
Next
This behavior is by design, VBScript can't do anything with a non-variant array, there was a KB article from Microsoft that explained this but it is not on-line anymore:
Q165967 - PRB: Script Error Occurs When Referencing Non-variant Array
An archived copy of the article can be found at:
https://ftp.zx.net.nz/pub/archive/ftp.microsoft.com/MISC/KB/en-us/165/967.HTM