简体   繁体   中英

How can I make a .delay function in a h3 with JQuery in Selenium?

I tried to made a click in an 'h3' that contains two 'divs' so I tried to make a loop to go through them but it only opens the second 'div'.

Just it opens both 'divs' when I put an alert which stop the application between both of them. It works but I want to make this without have to block the application with the alert because I'm using it with Selenium in a Java project of Eclipse.

I tried to make this with a .delay() function but it didn't works. I put here the code that I use for it:

$("#proveDiv > div > h3").each(function(i, obj){
      $(obj).delay(60000).fadeIn().click();
})

It only opens the second 'div', but I want to the test go through both 'divs' and not only on one of them.

I want to know if there is some function like delay or sleep that do this task but without blocking the application.

Any help would be appreciated. Thank you very much!

EDIT: I also tried with the function setTimeout but it didn't work also. Please let me know if I'm doing something wrong.

$("#proveDiv > div > h3").each( function(i) 
{
    setTimeout(function() 
    { 
       $(this).click()} , 10000)
    }  
)

Assuming you are executing this Javascript through Selenium and wants to test it. How about selecting and clicking them one by one

JavascriptExecutor driver = (JavascriptExecutor) webdriver;
driver.executeScript("$(`proveDiv > div > h3`).eq(0).click();");
TimeUnit.SECONDS.sleep(100); // or Thread.sleep(100);
driver.executeScript("$(`proveDiv > div > h3`).eq(1).click();");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM