2017-11-11 5 views
0

これを要約すると、私はStackOverflowで見つけた複数の異なるコードを試していて、どれも動作していないようです。ページ上のJavaScriptスクリプトでフォローボタンを自動的にクリックする方法

私の問題:

私は、Javascriptのコードをウェブサイト上のボタンを自動的にクリックできるようにしたいです。ここで

はコードです:

HTML:

<label class="button slim hollow secondary ">Follow</label> 

JS(私はこれを使用しようとしていますが、それは動作していない):

document.getElementsByClassName("button slim hollow secondary ")[0].click(); 

コンソールを使用してページにコードを実行すると、そのコードは機能しません、 何かアドバイス?

+1

エラーが表示されますか? –

+0

"undefined"は唯一のエラーです –

+1

"TypeError:document.getElementsByClassName(...)[0]は未定義です" –

答えて

0

HTMLが

window.onload = clicklabel(); 
 

 

 
function clicklabel(){ 
 
document.getElementsByClassName("button slim hollow secondary ")[0].click(); 
 
document.getElementsByClassName("button slim hollow secondary ")[0].style.color = 'red'; 
 
}
<label class="button slim hollow secondary ">Follow</label> 
 
<br> 
 
<label class="button slim hollow secondary ">Follow1</label> 
 
<br> 
 
<label class="button slim hollow secondary ">Follow2</label> 
 
<br> 
 
<label class="button slim hollow secondary ">Follow3</label> 
 
<br> 
 
<label class="button slim hollow secondary ">Follow4</label> 
 
<br> 
 
<label class="button slim hollow secondary ">Follow5</label> 
 
<br> 
 
<label class="button slim hollow secondary ">Follow6</label>

1

に作成されたときに、ウィンドウがロードされた後、クリックをするためにwindow.onload機能を追加し、それが意味する、あなたが複数のクラスを持っている..あなたは属性として単語クラスを扱うことができます
`

var btn = document.querySelectorAll('[class="button slim hollow secondary "]')[0]; 
btn.click(); 

`

0

document.querySelector(".class1.class2")メソッドを使用できます。

window.onload = doSomething; 
function doSomething() { 
    var btn = document.querySelector(".button.slim.hollow.secondary"); 
    btn.addEventListener("click", function(){ 
     console.log(this.innerHTML); 
    }); 
    btn.click(); 
} 

コンソールでは、結果として「フォロー」が表示されます。

ウェブページが完全に読み込まれると(画像を含む)、window.onloadはdoSomething関数を呼び出します。

関連する問題