Checkout our demo site to practice selenium https://magento.softwaretestingboard.com/

1 like 0 dislike
430 views
by Contributing Tester (91 points)
edited by
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 like 0 dislike
by Expert (572 points)

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;
            }
            
        } 
        } 


 

by Contributing Tester (91 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.
by The go-to Tester (181 points)
great! I am glad it worked!
by Contributing Tester (91 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
by The go-to Tester (181 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


This site is for software testing professionals, where you can ask all your questions and get answers from 1300+ masters of the profession. Click here to submit yours now!

1.4k questions

1.6k answers

866 comments

1.9k users

...