0

I would like to jump to a specific time while playing a video. While I have manage to achieve similar objective from YouTube website (see thread here), the same approach fail to work properly at a different website. As much I would like to share the website link, but due to confidential concern, Im unable to disclose it here.

The embedded video in this website have frame with the ID player. And I know the following line is working without any error.

        video = WebDriverWait(self.browser, 15).until(EC.visibility_of_element_located((By.ID, "player")))

Since, the iframe does not have a TagName, I instead replace the getElementsByTagName into getElementById. However, the compiler return an error

Message: javascript error: Cannot read property 'currentTime' of undefined

The full code is as below

   video = WebDriverWait(self.browser, 15).until(EC.visibility_of_element_located((By.ID, "player"))) # The line work perfectly
    print('Fast forward')
    player_status = self.browser.execute_script("document.getElementById('player')[0].currentTime += 80;") # The compiler return an error at this line.
    print('Get Current time')
    time_video = self.browser.execute_script("return document.getElementById('player')[0].currentTime;")

I appreciate for any hint or help.

Thanks in advance.

Community
  • 1
  • 1
mpx
  • 3,081
  • 2
  • 26
  • 56
  • there is no getElement**s**ById, there is `getElementById` *singular*. It returns a single element, not a collection. – VLAZ Apr 30 '20 at 13:46
  • Thanks for the response @vlaz. But, I got different error after changing it into singular. The new error is: Cannot read property 'currentTime' of undefined – mpx Apr 30 '20 at 13:49
  • 1
    Because *it returns a single element*! You are trying to get "the first" element from it `[0]`, this wouldn't work. – VLAZ Apr 30 '20 at 13:51
  • Thanks for pointing out. Appreciate it – mpx Apr 30 '20 at 13:54
  • For future reader> The line should be: 'player_status = self.browser.execute_script("document.getElementById('player').currentTime += 80;")' – mpx Apr 30 '20 at 13:54

1 Answers1

0

As pointed by @VLAZ, the syntax should be changed into

self.browser.execute_script("document.getElementById('player').currentTime += 80;")'
mpx
  • 3,081
  • 2
  • 26
  • 56