Discussion:
[selenium-users] how do i import javascript executor in python?
Enrico Bergamini
2016-04-12 12:42:51 UTC
Permalink
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
<https://stackoverflow.com/questions/36532523/selenium-unable-to-click-facebooks-like-button>
)
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
--
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+***@googlegroups.com.
To post to this group, send email to selenium-***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/986c3e2c-536c-45b6-ae7e-198f674c92c3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
David
2016-04-12 22:13:48 UTC
Permalink
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
Post by Enrico Bergamini
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
<https://stackoverflow.com/questions/36532523/selenium-unable-to-click-facebooks-like-button>
)
I load all the page with the multiple elements then I have 2 paths to find
buttons = driver.find_elements_by_tag_name("input")
ActionChains(driver).click(element).perform()
[object HTMLInputElement].
Probably because the element is invisible
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
--
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+***@googlegroups.com.
To post to this group, send email to selenium-***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/6ff1e725-473c-4043-bd7c-63e0ce424042%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Enrico Bergamini
2016-04-13 06:28:19 UTC
Permalink
Solved! Thank you very much!!
Post by David
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
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
Post by Enrico Bergamini
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
<https://stackoverflow.com/questions/36532523/selenium-unable-to-click-facebooks-like-button>
)
I load all the page with the multiple elements then I have 2 paths to
buttons = driver.find_elements_by_tag_name("input")
ActionChains(driver).click(element).perform()
[object HTMLInputElement].
Probably because the element is invisible
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
--
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+***@googlegroups.com.
To post to this group, send email to selenium-***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/4c6e8ea7-39e5-496b-9108-cbfaa95d03c4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
vishnu deep
2017-03-26 20:15:34 UTC
Permalink
hi david,

what is that document mean in the script??
Post by David
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
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
Post by Enrico Bergamini
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
<https://stackoverflow.com/questions/36532523/selenium-unable-to-click-facebooks-like-button>
)
I load all the page with the multiple elements then I have 2 paths to
buttons = driver.find_elements_by_tag_name("input")
ActionChains(driver).click(element).perform()
[object HTMLInputElement].
Probably because the element is invisible
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
--
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+***@googlegroups.com.
To post to this group, send email to selenium-***@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.
⇜Krishnan Mahadevan⇝
2017-03-28 04:49:34 UTC
Permalink
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!"
Post by vishnu deep
hi david,
what is that document mean in the script??
Post by David
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('comm
entable_item')[0].click();");
if you did want/have multiple elements, you'd have to iterate through
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
Post by Enrico Bergamini
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
<https://stackoverflow.com/questions/36532523/selenium-unable-to-click-facebooks-like-button>
)
I load all the page with the multiple elements then I have 2 paths to
buttons = driver.find_elements_by_tag_name("input")
ActionChains(driver).click(element).perform()
[object HTMLInputElement].
Probably because the element is invisible
driver.execute_script("document.getElementsByClassName('comm
entable_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
--
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
To view this discussion on the web visit https://groups.google.com/d/
msgid/selenium-users/75eedbad-f5cf-4286-a187-ec523b4361b4%
40googlegroups.com
<https://groups.google.com/d/msgid/selenium-users/75eedbad-f5cf-4286-a187-ec523b4361b4%40googlegroups.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
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+***@googlegroups.com.
To post to this group, send email to selenium-***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/CANikZL%3DpHGbtdW5PXYS-RZGEp%2B-cCct-sQO2txXQFL%2BLNoNSwg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Loading...