1

Lately I have written the following code to click on any of the following elements which is visible:

Purpose : It is basically a never ending loop to repeat the same action again i.e, click 3 buttons.

Problem : When I run the code it seems to take arbitrary time between 1 second- 7 seconds to click a button, tough page gets loaded successfully in an instant. Hence I am eager to know what is it in the code which the delaying the operation? and Is there any efficient way to reduce the time?

     battle(WebDriver driver1)
{
    try {           if(driver1.findElements(By.xpath("....")).size()!= 0)
            {
                driver1.findElement(By.xpath("....")).click();

            }       
            if(driver1.findElements(By.xpath("....")).size()!= 0)
            {
                       driver1.findElement(By.xpath("....")).click();

            }       
            if(driver1.findElements(By.xpath("....")).size()!= 0)
            {
                       driver1.findElement(By.xpath("....")).click();

            }       
            battle(driver1);
            }

        catch(Exception e)
        {
            battle(driver1);
        }
}
Krish Krishna
  • 317
  • 3
  • 15

2 Answers2

0

here you are using xpath to click on button. generally xpath will take more time compare to ID and CSS. Please give try by taking ID and CSS and then u can check the difference in execution time. You can refer to this link to understand why xpath takes more time for execution.

Shital Mokashi
  • 149
  • 1
  • 1
  • 8
  • 1
    This should be a comment, not answer. – Vinit Mehta Jun 08 '18 at 12:29
  • 1
    unable to comment because my reputation score is less than 50 – Shital Mokashi Jun 08 '18 at 12:35
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). – undetected Selenium Jun 08 '18 at 12:40
0

If I were to guess, you probably have an implicit wait set. Any time you look for an element that doesn't exist, Selenium will wait for the implicit time (5s or whatever is set) until the timeout is reached and then move on. My suggestion is to remove the implicit wait completely and see how it goes. If you need a wait, add in a WebDriverWait for each scenario.

Another, maybe minor issue, is that for each element to be clicked you are scraping the page twice... once to see if it exists and then again to click the element. You can alter your code to only scrape the page once, store the results in a variable, check to see if the collection is empty, and click the element if it's non-empty.

Since you are repeating code 3 times, I would write a function that handles that code and then call it as needed.

There are some other things that I've changed. One example is that you seem to want to always run battle no matter what. Rather than recursively calling battle and calling battle after an exception, just add a while loop that never ends. Having said that, once you polish this code up and use it somewhere you will likely want to add an escape... some way for the user to exit the program and your while would depend on that flag being set but that's another discussion.

battle(WebDriver driver1)
{
    while (true)
    {
        clickIfExists(By.xpath("xpath1"));
        clickIfExists(By.xpath("xpath2"));
        clickIfExists(By.xpath("xpath3"));
    }
}

public void clickIfExists(By locator)
{
    List<WebElement> e = driver1.findElements(locator);
    if (e.size() > 0)
    {
        e.get(0).click();
    }       
}
JeffC
  • 22,180
  • 5
  • 32
  • 55