1
driver.get(link)
time.sleep(2)
lol = driver.find_element_by_xpath("//*[@id='table_div']/div/div[1]/table/tbody/tr[2]/td[10]/div/div")
hover = ActionChains(driver).move_to_element(lol)
hover.perform()

Here is the code I have so far. I am trying to get some text that only shows when I hover over a certain element. I found the element with selenium, and am hovering over it, but I can't figure out how to actually get the data. It looks like the data is not there when I inspect element, and I can only see it in the javascript when I view the page source. It's just a variable, so how do I get that variable value from the javascript?

javascript:

   function apply_pop(){
   jq(".pop_up").mouseenter(function(p_event){
   p_value = jq(this).attr("value")
   //creating dynamic lists from python static storage
   var data_list = ['0', '2015-07-13 17:29:15'];

   var desp_list = [['RUNNING 1000.0 None', 'INVALID None None', 'INVALID None None', 'PASSED 0.05 2015-07-10 17:21:54', 'FAILED 0.05 2015-07-08 12:35:55', 'FAILED 0.05 2015-07-08 09:54:48', 'FAILED 0.05 2015-07-07 18:21:17', 'FAILED 0.05 2015-07-07 17:07:50', 'FAILED 0.05 2015-05-28 18:33:41'], ['FAILED 0.05 2015-07-13 16:33:38', 'FAILED 0.05 2015-07-10 15:36:30', 'FAILED 0.05 2015-07-09 19:39:46', 'FAILED 0.05 2015-07-09 11:00:45', 'FAILED 0.05 2015-07-09 01:05:13', 'INVALID None None', 'INVALID None None', 'FAILED 0.05 2015-06-19 22:42:21']]
   ...};

I want the first thing in the desp_list (RUNNING 1000.0 None)

rishubk
  • 441
  • 2
  • 8
  • 19
  • Could you show where and how this variable is defined? Also, can you catch the HTML markup when the element is hovered and point where exactly your desired data is located. Thanks! – alecxe Jul 14 '15 at 05:15
  • I added the javascript, but the html is the problem. I don't think the data actually shows up in the html when I hover. It's only in the javascript. – rishubk Jul 14 '15 at 05:28

1 Answers1

0

We can argue about the reliability of this approach, but what if you just get the page source and extract the desired data via regex. Usually, parsing HTML with regular expressions is not a good idea, but here we don't care about the structure of the page and need only a part of javascript array. Sample code (not tested):

import re

# ...

page_source = driver.page_source

pattern = re.compile(r"var desp_list = \[\['(.*?)',")
match = pattern.search(page_source, re.MULTILINE | re.DOTALL)
print match.group(1) if match else "No match"
Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • thanks. is what you have in the parentheses correct though? Do I need to add RUNNING 1000.0 None? I don't really know how to use regex – rishubk Jul 14 '15 at 05:44
  • yea its giving me no match – rishubk Jul 14 '15 at 05:46
  • i think it didn't work cause i cut off too much from the javascript. please look at the edited code above, and let me know if you know how to fix it – rishubk Jul 14 '15 at 06:19
  • @rishubk ah, sure, there are 2 square brackets. I've updated the answer, try it out. Thanks. – alecxe Jul 14 '15 at 12:20
  • sorry to ask you another question, but could you please explain the syntax of the re.compile statement. I read the regex documentation online, and it is still not making sense – rishubk Jul 15 '15 at 00:51
  • @rishubk sure, the key thing to understand is that `(.*?)` part - parenthesis define a saving/capturing group - a way to extract a part of matching text. `.*?` is a non-greedy match for any characters. The backslashes in the pattern are just for escaping - `[` has a special meaning in regular expression and needs to be escaped..hope that makes things a bit more clear. – alecxe Jul 15 '15 at 01:03