1

I'm trying to speed up some slow code by separating page loads from page analysis, but it seems that this breaks some rules that Selenium usually follows. As I understand it (found here) when you find something, Selenium stores references to it, not the actual thing you've found.

The result of this behaviour is that you can't load another page before processing the things you found. ie this sequential example works:

myDriver.Navigate.GoToUrl("mysite")
myVar = myDriver.FindElementsByClassName("upperContainer")
thisPN(i) = Trim(myVar.FindElement(By.ClassName("partNumber")).Text)
myDriver.Navigate.GoToUrl("http://www.google.com")

In order to make it faster though, I'm trying to pass the found items to a new thread for processing while the driver does other page loads.

This is illustrated by the below code which throws an exception An unhandled exception of type 'OpenQA.Selenium.StaleElementReferenceException' occurred in WebDriver.dll Additional information: stale element reference: element is not attached to the page document

sub main()
   myDriver.Navigate.GoToUrl("mysite")
   myVar = myDriver.FindElementsByClassName("upperContainer") 

   Dim myThread As New Thread(Sub() ripitems(myVar))
   myThread.Start() 'this line kicks off the sub
   myDriver.Navigate.GoToUrl("anothersite") ' and then this line changes the page before the sub has got going
end sub

sub ripitems(ByVal elementCollection As System.Collections.ObjectModel.ReadOnlyCollection(Of IWebElement))
   for i = 0 to 100
      thisPN(i) = Trim(elementCollection.FindElement(By.ClassName("partNumber")).Text)
   next i
end sub

I can cure this exception by adding an arbitrary Threading.Thread.Sleep(500) in after myThread.Start(). So I know it's the page change, not the threading that breaks things.

Is there any function that copies the found HTML into the result variable, so that I can use it even after the page I'm looking at has changed. Something along the lines of:

myVar = **ByVal** myDriver.FindElementsByClassName("upperContainer")
Community
  • 1
  • 1
Martin KS
  • 481
  • 7
  • 26
  • Just re-define your `myVar` in same way after redirection to new page – Andersson Jan 11 '17 at 17:01
  • But I want to use the things in myVar from the first site - I think my example code isn't clear, sorry I'll change it a bit. – Martin KS Jan 11 '17 at 17:03
  • 2
    You can get some data from list of elements from first page (eg text values or attributes) to use it on next page, but you cannot handle web-elements from one page on another – Andersson Jan 11 '17 at 17:07
  • The problem is that I want to get lots of text values and attributes, from a big page, and that halts the program for 15s. If I was able to pass a collection of blocks of HTML for further analysis then that could be done by a daughter thread, and half the execution time of the code. – Martin KS Jan 11 '17 at 17:15
  • 2
    You can scrape `HTML` page with `HTTP` GET-request and then handle it with some `HTML`-parser – Andersson Jan 11 '17 at 18:31
  • What about splitting up the validation among separate WebDriver instances? Otherwise, Andersson is correct: you can't access element references on one page while on another page (after the DOM has lost the elements). – jibbs Jan 11 '17 at 18:49
  • Nonetheless, this was a great, interesting question and I appreciate what you were trying to accomplish! – jibbs Jan 11 '17 at 18:53
  • Thanks all, looks like a small re-write is in order to use more browsers instead of trying to hand-off parsing. – Martin KS Jan 12 '17 at 11:49

0 Answers0