how do i import javascript executor in python?

3,401 views
Skip to first unread message

Enrico Bergamini

unread,
Apr 12, 2016, 8:42:51 AM4/12/16
to Selenium Users
So I'm trying to click on multiple like buttons on facebook, which is difficult cause clickable elements are hidden inputs in a form. (full details here
I load all the page with the multiple elements then I have 2 paths to find and click like buttons:

buttons =  driver.find_elements_by_tag_name("input")
for element in buttons:
   
ActionChains(driver).click(element).perform()

This returns: selenium.common.exceptions.MoveTargetOutOfBoundsException: Message: Offset within element cannot be scrolled into view: (0, 0): [object HTMLInputElement]. 
Probably because the element is invisible

Or I try to inject JS:
driver.execute_script("document.getElementsByClassName('commentable_item').click();");


But it returns that the function document.getElementsByClassName(...).click() is undefined. Why? Should I use any import to make execute_script work?


from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
from selenium.webdriver.common.action_chains import ActionChains

David

unread,
Apr 12, 2016, 6:13:48 PM4/12/16
to Selenium Users
You forget that the base method you are executing returns an array, only getElementById() returns singular, all the other document.getXyz methods return plural/array, even if the result is a single result. Therefore, you need to add an index to the call, e.g.

driver.execute_script("document.getElementsByClassName('commentable_item')[0].click();");

if you did want/have multiple elements, you'd have to iterate through that array and click each element:

var elems = document.getElementsByClassName('commentable_item');
for(var i= 0;i<elems.length;i++){
 elems
[i].click();
}//and execute this whole block of code with execute_script


Enrico Bergamini

unread,
Apr 13, 2016, 2:28:20 AM4/13/16
to Selenium Users
Solved! Thank you very much!!

vishnu deep

unread,
Mar 27, 2017, 2:45:26 AM3/27/17
to Selenium Users
hi david,

what is that document mean in the script??

⇜Krishnan Mahadevan⇝

unread,
Mar 28, 2017, 12:50:08 AM3/28/17
to Selenium Users
The document object being referred is this one : https://www.w3schools.com/jsref/dom_obj_document.asp

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-users+unsubscribe@googlegroups.com.
To post to this group, send email to selenium-users@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/75eedbad-f5cf-4286-a187-ec523b4361b4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages