1

I am trying to remove all the elements with the class "RemoveByMe", by executing javascript on an open browser. I previously created those elements myself.

I'm calling that javascript from Python: Selenium controls the browser, and sends the javascript to the open browser. But I think the problem relies on the javascript side, reasons on the next lines.

The python code for executing each script is:

Browser.execute_script("script here")

So far I have tried the following javascript code:

$('.RemoveByMe').remove();

This one gives the following error message:

#WebDriverException: Message: u'$ is not defined' ; Stacktrace: 
    at anonymous

This error seems to come from the python side, so I have tested more javascript code:

var DocElements = document.getElementsByTagName('RemoveByMe');for(var i = 0; i < DocElements.length; i++){DocElements[i].parentNode.removeChild(DocElements[0]);}

This one doesnt throw any errors (confirming that the python side is working), but still doesnt remove the elements. (javascript is not being able to remove them)

All of the tested javascript code works in some websites, but it doesnt in some others. That makes me think that the problem resides in the website, preventing the javascript from removing elements. (Although I could successfully add them)

How could I skip that prevention?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
I want badges
  • 6,155
  • 5
  • 23
  • 38

2 Answers2

1

First one obviously seems that jQuery is not defined.

The 2nd one looks close though you would need document.getElementsByClassName('RemoveByMe') ... instead of by getElementsTagName as you want to get elements by a CSS class.

jwwishart
  • 2,865
  • 2
  • 23
  • 26
  • Thank you. Yes, it actually was a miss-match between "by_Tag", "by_Class", and the actuall class name I was searching for. Thanks for pointing out, that made me re-look for all of them and solved it :) – I want badges Feb 08 '14 at 10:54
0
$('.RemoveByMe').remove();

That snippet is jQuery Code, a Javascript Library.

A solution for your problem you can find in this thread: Remove all elements of a certain class with JavaScript

Community
  • 1
  • 1
D. Schalla
  • 655
  • 4
  • 9
  • Thank you. I had already looked at that, so I actually run the given code in a loop until there arent more matches. The fact is that the code sucesfully removes those elements from some websites (all of them) and in the given link it just cant, and same hapens with the other script. https://www1.sedecatastro.gob.es/CYCBienInmueble/OVCBusqueda.aspx – I want badges Feb 08 '14 at 10:48