簡體   English   中英

如何以編程方式模擬用戶在此特定網站上的點擊?

[英]How to simulate user click programmatically on this specific website?

我正在嘗試在日期選擇器樣元素上使用javascript觸發點擊。 這旨在用於puppeter無頭瀏覽器集成,但是首先我需要我的代碼在devtools控制台中工作。

但是,它不起作用...與正常的手動單擊不同。 我已經嘗試了好幾個小時了

特定的網站在這里(使用react)

https://www.homeaway.co.uk/p4787689

您必須單擊“入住”,以顯示模式日期選擇器,然后我的意圖是對日期數字之一執行javascript單擊。 “ 1”,“ 2”,“ 3” ...在下面可見

這是日期選擇器的外觀

單個日期條目(我們要單擊)的html結構如下:每個日期(01.08.2019、02.08.2019等)為“ td”行。 該類的名稱甚至具有“ clickable”。 因此,它應該是我們要單擊的元素。

<td class="day no-gridlines selectable clickable" data-column="4" data-row="1" tabindex="-1" aria-label="August 9, 2019" aria-hidden="false">

    <div class="calendar-template calendar-template--show-availability calendar-template--show-selectability">
        <div class="day-template day-template--available-stay day-template--available-checkin-checkout">
            <div class="day-template__border">
            </div>
            <div class="day-template__content">
                <div class="day-template__day">9</div>
            </div>
        </div>
    </div>
</td>

我試圖獲取每個TD元素:

document.querySelectorAll('.day.no-gridlines.selectable.clickable')

然后單擊其中之一

document.querySelectorAll('.day.no-gridlines.selectable.clickable')[4].click()

但是它不起作用嗎?

我試圖檢查事件單擊處理程序,但是該網站正在使用某些框架..因此,對於像我這樣的初學者來說,這非常困難。

也許更有經驗的人可以提供建議,為什么我無法在該網站上執行點擊?

如果您顯示一個頁面,這些可點擊元素在哪里會更好(因為在您的鏈接上我什至找不到可點擊類),無論如何,請在控制台中嘗試以下操作:

javascript:function AutoClickOnThese()
{
    var targets = document.querySelectorAll(".clickable");
    Array.prototype.forEach.call(targets, function(target,index){
        //If the clicks start some ajax request, these sure will be restricted
        //if too much running in an unrealistic time
        //... if there are no ajax, just delete the timeout stuff
        setTimeout(function()
        {
            target.onclick();
            //target.click(); //if not working the onclick
        }, index*600);
    });
};
AutoClickOnThese();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM