I'm trying get the text value of clicked element (any element) on the website, return it from javascript to Selenium (Python) and use that information to put every clicked element in my log. Is that even possible to achieve using selenium and javascript?
This function can display the element, but won't return anything (normal onclick behaviour). I know that I can read text from alert pop-ups using selenium, but it will make my browser flash on every mouse click.
function getEl()
{
var ev = arguments[0] || window.event,
origEl = ev.target || ev.srcElement;
alert(origEL.text)
}
document.onclick = getEl;
Trying to access javascript console.log doesn't seem to work in Selenium right now so using console.log is not the right answer.
I can detect when a certain element was clicked by writing something to localstorage on click event and then checking the value in localstorage in python. But it's not universal. I need to find and set up every element that I want to observe.
function find_element()
{
aTags = document.getElementsByTagName("a");
searchText = "comments";
for (var i = 0; i < aTags.length; i++)
{
if (aTags[i].textContent == searchText)
{
found = aTags[i];
return found;
}
}
}
found=find_element();
function saveEvent()
{
localStorage.setItem("ZAD1.1", "1");
}
if(found)
{
found.addEventListener("click",saveEvent,false);
var x=localStorage.getItem("ZAD1.1");
if (x=="1")
{
count="comments link was clicked";
return count;
}
}
After that you invoke javascript from python selenium
z=driver.execute_script(javascript1)
if z=="comments link was clicked":
#do something with this information
Is there any way to get information about objects clicked by user in the browser? I'm using Selenium with Firefox.
EDIT: You can get every clicked element by using getEL() and writing every onclick output in localstorage.
localStorage.setItem(origEl.text, origEl.text);
Create local storage and then write temp as array or string
var temp="VISITED LINKS : "
for (var i = 0; i < localStorage.length; i++){
temp +=localStorage.getItem(localStorage.key(i))+" ";}
return temp
Then you can return the entire list to python.
Can you think of any other way to send clicked objects to python?