1
Unable to click on the User name searched from the Look up field

Open 1 Answers 10 Views
Unable to click on the User name searched from the Look up field. Tried by giving exclipit wait but not working. Please help me with the code. Attached code is below:

public class AddFollowerTask {
        WebDriver driver ;
        
        public AddFollowerTask(WebDriver driver)
        {
            this.driver = driver;
        }
        
        public void addFollower() {
            driver.findElement(By.xpath("//*[@id=\"crumb-right\"]/ul/li[3]/span[2]/a")).click();
           driver.findElement(By.xpath("//*[@id=\"crumb-right\"]/ul[1]/li[3]/span[2]/div/div[2]/div[1]/div/input")).sendKeys("a");

            try{
                (new WebDriverWait(driver, 6/*sec*/)).
                        until(ExpectedConditions.elementToBeClickable((By.className("clearfix "))));          
                        }
            catch(org.openqa.selenium.TimeoutException e){
                System.out.println(e.getMessage());
            }
            
         List<WebElement> autoList = driver.findElements(By.className("list-unstyled ac-list"));
         System.out.println(autoList.size());
         
        
         if(autoList.size() == 0) {
             System.out.println("Results not found");

        }
        else {
             System.out.println("List found with elements " +autoList.size());
        }
        
        for(WebElement al : autoList)
        {
            if(al.getText().contains("Armin")) {
                 al.click();
                    break;
            }
            
        }
        }

    }

1 Answer

0

This is a little tricky one.

So first thing first,

below block will not work, so remove it.

try{
                (new WebDriverWait(driver, 6/*sec*/)).
                        until(ExpectedConditions.elementToBeClickable((By.className("clearfix "))));          
                        }
            catch(org.openqa.selenium.TimeoutException e){
                System.out.println(e.getMessage());
            }

because I can see that By.className("clearfix ") contains space and that will throw an exception for sure. That exception we are not throwing it back to the code and simply printing on console. So, that exception will never show up as an exception.

Second thing, as per my experience, clearfix is a very common class name. I am sure more than one element will be using that class name. So, again the code will fail either with an exception saying "class name can not have space" or your wait will simply pass because element if identified.

The right thing to do is, identify the autoList

List<WebElement> autoList = driver.findElements(By.className("list-unstyled ac-list"));


and wait until the size of this autoList is greater than 0.

Something like,

List<WebElement> autoList = driver.findElements(By.className("list-unstyled ac-list"));
do{
Thread.sleep(1000); //This thread.sleep will make thread wait for one second. It does not really harm and its good practice.
autoList = driver.findElements(By.className("list-unstyled ac-list"));
}while(autoList.size()>0);

Once it has some elements, then you can loop through using while,

for(WebElement al : autoList)
        {
            if(al.getText().contains("Armin")) {
                 al.click();
                    break;
            }
            
        } 
        } 


 

answered 5 days ago by SunilBhaskar (427 points)
4Comments
commented 5 days ago by Priyanka91 (89 points)
Thanks Sunil. I found where I was wrong. Yes, you are right! 'Clearfix' is very common and it had many sub-elements. Used some generic class name like 'item-name-inner' name to over the issue.
commented 5 days ago by stbadmin (4,950 points)
great! I am glad it worked!
commented 5 days ago by Priyanka91 (89 points)
Hi Sunil,

For the below code:

public class CommentTask {
    WebDriver driver;
   
    public CommentTask(WebDriver driver)
    {
        this.driver = driver;
    }
   

    public void commentOnTask()
    {
        WebDriverWait wait = new WebDriverWait(driver, 10);
        try {
             
         wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("text_div")));
           
        }
        catch(org.openqa.selenium.TimeoutException e){
            System.out.println(e.getMessage());
               
        }
       
        driver.findElement(By.className("text_div")).sendKeys("Test Comment");
    WebElement submitButton =    wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\"comments-task-6-59ba23e91bb06\"]/div/div/div[3]/div[2]/form/button")));
    submitButton.click();
   

    }

}

Am able to enter text into the Comment field but unable to find the path of Submit button and click on it. getting below exception:
WARNING: WebDriverException thrown by findElement(By.xpath: //*[@id="comments-task-6-59ba23e91bb06"]/div/div/div[3]/div[2]/form/button)
org.openqa.selenium.WebDriverException: Failed to decode response from marionette.

Can you please help me with this
commented 5 days ago by stbadmin (4,950 points)
This is a new issue. New issue should be posted as a new question. I will get it posted as a new question.
https://softwaretestingboard.com/qna/2401/warning-webdriverexception-thrown-by-findelement-by-xpath
...