Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: Cannot read property 'click' of null

I have been mass following / unfollowing / favoriting / unfavoriting on twitter with these codes ;

$('button.follow-button').click();
$('button.ProfileTweet-actionButtonUndo').click();
$('a.favorite').click();

I found some not-working codes online and tried to fix, so they are nearly working now. The only thing is to go to the page on google chrome, press f12 , open console and run the command.

So now, I wanted to use the same system in Vine. There"s an extension for google chrome to use vine ( https://client.vineclient.com/ ) and I am trying to use it because original webpage ( vine.co ) is not even useful.

The only difference between twitter and this, is that, in vine client for ^followers^ section, an pop-up comes in the page. And I don!t know if it detects the buttons.

So , the code of the button I am trying to click from original page , ( I took it using developer tools / elements )

<div class="user">
<img alt="Tuğçe Nur TEKİN" class="avatar" src="http://v.cdn.vine.co/r/avatars/3C2E4F40F11066473630650847232_pic-r-1397241239141e51579937f.jpg.jpg?versionId=ttIz4CExCjW_4TEnxYbnehPPpXRyzMco">
<div class="wrapper">
<a class="username" 
href="https://client.vineclient.com/explore/users/1066473613911363584">
Tuğçe Nur TEKİN
</a>
</div>
<a class="button follow" data-id="1066473613911363584">
<i class="icon">
</i>
</a>
</div>

The thing to click is here :

When I manually click, it changes into this :

<a class="button unfollow" data-id="1066473613911363584"><i class="icon"></i></a>

And the command I use to try to click

$(".a.follow").click();

The error I get,

TypeError: Cannot read property 'click' of null

How can I fix that? By the way, in these codes, I couldn"t find actually any ^button^ codes.. Thanks!

UPDATE

  1. http ://prntscr. com/3pmo2i
  2. http ://prntscr. com/3pmoyk
  3. http ://prntscr. com/3pmpym
like image 294
Berque Cemilian Avatar asked Jun 04 '14 17:06

Berque Cemilian


People also ask

What is TypeError Cannot read properties of null?

The "Cannot read property 'value' of null" error occurs when: trying to access the value property on a null value, e.g. after calling getElementById with an invalid identifier. inserting the JS script tag before the DOM elements have been declared.

Can not set property of null?

To solve the "Cannot set property of null" error, make sure that the DOM element you are accessing exists. The error is often thrown when trying to set a property after using the getElementById() method and passing it a non-existent id. Copied! const el = document.

What does Cannot read property toLowerCase of null mean?

The "Cannot read Property 'toLowerCase' of null" error occurs when the toLowerCase() method is called on a variable that stores a null value. To solve the error, make sure to only call the toLowerCase method on strings.

Can't access property value document getElementById is null?

This error TypeError: document. getelementbyid(...) is null would seem to indicate that there is no such element with an ID passed to getElementById() exist. This can happen if the JavaScript code is executed before the page is fully loaded, so its not able to find the element.


3 Answers

I had encountered this problem, when I run javascript on the webpage from elsewhere (eg. Selenium) while simultaneously using Developer Tools. Try closing Developer Tools and run the script.

like image 126
premganz Avatar answered Oct 21 '22 00:10

premganz


In order to use the jQuery library, you need to make sure that the library has been loaded on the page. As @adeneo mentioned, jQuery does not return null values, which means the $ variable is holding reference to an object that isn't jQuery. To verify that jQuery is loaded on a page, enter typeof jQuery into the chrome debugger. If jQuery is loaded on the page, the console will output "function". Otherwise, it will output "undefined". Steps for adding jQuery to your page can be found here.

Once you have verified that jQuery is properly loaded on the page, you will have to correct the CSS selector that you are using to access the desired element.

$(".a.follow") means "select all elements that have both classes 'a' and 'follow'".

<div class="a follow"></div>

Instead, your selector should be $("a.follow"), which means "select all 'a' type elements that have the 'follow' class".

<a class="follow"></a>
like image 33
tommyTheHitMan Avatar answered Oct 20 '22 22:10

tommyTheHitMan


In my case the solution was to simply turn off the element-inspector within my browser! :)

like image 30
FranBran Avatar answered Oct 20 '22 23:10

FranBran