0

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!

  • The link you have provided does not work for non-Amazon employees :-( If you can share the page html via pastebin.com (without confidential info) and the relevant parts in using snippet tool via [edit] we might suggest other selectors for element. I suspect that is not the problem though. – QHarr Feb 28 '19 at 18:45
  • Appreciate the response - I'm not going to be able to share the actual page. I can explore grabbing it via another element but curious why the xpath isn't working. Unfortunately there isn't much other info to grab from - the class is randomly generated and there isn't an ID. I could possibly do it based off the index but would love to try and get it working via xpath as that seems the most reliable in this case. – Mathias Miharkula Feb 28 '19 at 19:17
  • Don't know the ins and outs but not sure why xpath would be more reliable than css unless you next to search by text. It's almost certainly less efficient and slower. – QHarr Feb 28 '19 at 19:22
  • Originally I had thought that the element was randomly generated but I'm seeing that might not be the case.
    (ID: 14962738)
    That's the element in question.
    – Mathias Miharkula Feb 28 '19 at 19:31
  • Does the class change over time? If it does, is there any part of the class value that remains constant? – QHarr Feb 28 '19 at 19:36
  • I'm not sure if it changes over time, but in looking at multiple searches it seems that the e15zxa0z0 is constant. I'm a bit unfamiliar with finding elements by class name or css - do you know if it's possible to pull the innertext for that element by using that e15zxa0z0 constant? – Mathias Miharkula Feb 28 '19 at 19:51
  • open that page and then press F12, select any element in html so highlighted then press Ctrl + F, in the search box enter the following: [class$='e15zxa0z0'] and press return. Do you get any matches returned? – QHarr Feb 28 '19 at 19:53
  • Yeah, unfortunately upon closer inspection it seems that the e15zxa0z0 aspect is used several times throughout the page. I didn't see it before because I was looking at the single element that I was trying to pull. Looking through to see if there is any other unique identifiers and it does look like the Index is always 342 and the Div is always [133]. I don't have experience with scraping anything other than xpath but curious if it might be more reliable to use one of those identifiers instead? – Mathias Miharkula Feb 28 '19 at 20:04
  • It doesn't matter if used several times as you could match on index of the returned collection, or loop the collection examining the .Text, or I would normally look for another element that I can use to specify a relationship e.g. parent child where the css match is one element. – QHarr Feb 28 '19 at 20:07
  • Index is always 342 ? Index of what (sorry!) ? And I would be wary with just using an index on Divs alone. Just like xpath it can break very easily when a change is made. That is why css is often more robust. – QHarr Feb 28 '19 at 20:09
  • Once again, really appreciate the help here. I'm not a programmer by profession but this will help me tremendously to figure out. If I do a search for [class$='e15zxa0z0'] on the given page, the result I want is the first one that comes up. How would I go about pulling that using CSS? – Mathias Miharkula Feb 28 '19 at 21:12
  • FindElementByCss("[class$='e15zxa0z0']"). Sorry wrote in wrong language there initially. – QHarr Feb 28 '19 at 21:15
  • Let me know if that works. You then use the .Text property – QHarr Feb 28 '19 at 21:41
  • Fantastic thanks! I ended up going with SearchResult = bot.FindElementByCss("[class$='e15zxa0z0']").Attribute("innerText") and it works. I'm still curious why the Xpath isnt working but the reccomended solution works great so I appreciate the help! – Mathias Miharkula Feb 28 '19 at 22:04
  • Mind if I post an answer? – QHarr Feb 28 '19 at 22:16

1 Answers1

1

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

QHarr
  • 83,427
  • 12
  • 54
  • 101