0
  • I am using the below code snippet to Verify the visibility of an Element. whenever the mybuyers element is not present then selenium is taking too much time and not continuing the next step.
  • In below script catch block is printing the message "My Buyers link is not displayed" but not moving to next steps of the script.
WebElement Find=null;
Find=driver.findElement(By.xpath("//*[@id='headerapp']/div/div/ul/li[2]/a"));
if(Find!=null && Find.isDisplayed()){
    Actions action=new Actions(driver);
    action.moveToElement(Find).build().perform();
    driver.findElement(By.xpath("//*[@id='headerapp']/div/div/ul/li[2]/ul/li[1]/a")).click();
    driver.manage().timeouts().implicitlyWait(15000, TimeUnit.SECONDS);
}
Thread.sleep(2000);
WebElement mybuyers=null;
try{
    mybuyers=driver.findElement(By.xpath("//a[contains(., 'My Buyers')]"));
    if(mybuyers!=null && mybuyers.isDisplayed()){
        Actions action=new Actions(driver);
        action.moveToElement(mybuyers).build().perform();
        mybuyers.click();
        driver.manage().timeouts().implicitlyWait(15000, TimeUnit.SECONDS);
    }
}catch(Exception e){
    System.out.println("My Buyers link is not displayed");
}
finally{
    driver.manage().timeouts().implicitlyWait(15000, TimeUnit.SECONDS);
}
Thread.sleep(2000);
WebElement mylisting=null;
try{
    mylisting=driver.findElement(By.xpath("//a[contains(., 'My Listing')]"));
    if(mylisting!=null && mylisting.isDisplayed()){
        Actions action=new Actions(driver);
        action.moveToElement(mylisting).build().perform();
        mybuyers.click();
        driver.manage().timeouts().implicitlyWait(15000, TimeUnit.SECONDS);
    }
}catch(Exception e){
    System.out.println("My Listing link is not displayed");
}
finally{
    driver.manage().timeouts().implicitlyWait(15000, TimeUnit.SECONDS);
}
1
  • why are you using an implicit wait? Commented Nov 14, 2016 at 4:21

1 Answer 1

1

Use FindElements instead of FindElement like this:

 Boolean isMyBuyersPresent = driver.findElements(By.yourLocator).size() > 0

And

if(isMyBuyersPresent)
{
    //You can re-find as below, or refactor the code and 
    //create a list in the above snippet and grab the first element here

    mybuyers= driver.findElements(By.yourLocator)
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.