0

enter image description hereI am trying to locate an element from list of items that are saved in List. However when I try to click that element by giving its index number I am getting error "Element not clickable". Here is my code:

package TestCases;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.io.IOException;
import java.util.List;

import Utility.BarneyTestData;
import Utility.Constants;
import Utility.UtilityMethods;

public class AnonymousUserPurchase {
    // static WebDriver driver;
    UtilityMethods util = new UtilityMethods();

    @BeforeClass
    public void launchBrowser() {

        UtilityMethods.openBrowser(Constants.BROWSER_NAME);
        UtilityMethods.launchWebsite(Constants.URL);

    }

    @Test

    public void PurchaseItemTest() throws InterruptedException, IOException {
        Thread.sleep(9000);

        util.clickElement(Constants.MENCATEGORYTAB);
        util.clickbyXpath(Constants.MENTHIRTS);

        List<WebElement> element = util.getdriver().findElements(By.className(BarneyTestData.getValueOfExcel(0, 1)));
        System.out.println(element);
        Thread.sleep(5000);

        element.get(1).click();
    }

}

//Html code for the page

<a href="/product/alpha-industries-thedrop-40barneys-3a-m-65-defender--22love-trumps-hate-22-field-jacket-505380835.html" class="brand-link" precog_scanned="true">
                    Alpha Industries
                </a>


<a href="/product/alpha-industries-thedrop-40barneys-3a-m-65-defender--22love-trumps-hate-22-field-jacket-505380835.html" class="name-link" precog_scanned="true">thedrop@barneys: M-65 Defender "Love Trumps Hate" Field Jacket</a>
  • Chris, based on the fact that you mention it's a list and I see that you've imported the Selenium Select library, I'm guessing you're trying to access a select element, but improperly. Posting HTML code would have helped, but with all due respect, I think you should consider taking an on-line selenium course. – Bill Hileman May 07 '18 at 18:46
  • I was trying to use Select class before, But deleted that coded. However forgot to delete unused imported class. – chris stewart May 07 '18 at 18:48
  • HTML/Url something more to work with. – QHarr May 07 '18 at 18:48
  • You were probably on the right track, Chris, it probably is a select, but we can't tell without seeing the HTML source for the page. – Bill Hileman May 07 '18 at 18:50
  • @Bill I have pasted the html code for the element, Please check. – chris stewart May 07 '18 at 18:54
  • Load the web page manually, then right-click on the element and select "inspect" If you're using Chrome it will display the code fragment of the page source we'd need to see. Copy and paste it to your original question and mark it as code, do NOT post a screenshot. – Bill Hileman May 07 '18 at 18:54
  • If I have pasted some wrong html code, Please tell if i have to paste the code for the whole page or just for the element.And how to paste that for the while page if it is required. – chris stewart May 07 '18 at 18:55
  • Add a screenshot of the main page (not the source code) as well. Based one what you posted it might not be a Select. – Bill Hileman May 07 '18 at 18:56
  • Yes we don't need to use Select. – chris stewart May 07 '18 at 18:57
  • @Bill i have added the screenshot as per asked by you. – chris stewart May 07 '18 at 19:04
  • @DebanjanB if I use wait, I am getting this error "java.util.ArrayList cannot be cast to org.openqa.selenium.By" – chris stewart May 08 '18 at 02:36
  • I have made some changes, List element = util.getdriver().findElements(By.className(BarneyTestData.getValueOfExcel(0, 1))); System.out.println(element); Thread.sleep(5000); WebDriverWait wait2 = new WebDriverWait(util.getdriver(), 10); wait2.until(ExpectedConditions.elementToBeClickable((By) element)); – chris stewart May 08 '18 at 02:37
  • @chrisstewart You are trying to invoke `WebDriverWait` on a _List_. But `WebDriverWait` is applied for an _WebElement_. So you see the error. – undetected Selenium May 08 '18 at 07:03
  • I have removed that error by selecting any one element from list.For example- element.get(1).click();. But now I am getting error that "element is not clickable at some point" – chris stewart May 08 '18 at 10:26

2 Answers2

0

As according to Error, You can use action class to click element

Syntax:

Actions action = new Actions(driver);
action.moveToElement("Your Element").click().perform();
Ishita Shah
  • 3,955
  • 2
  • 27
  • 51
0

For that purpose, Selenium simulates a real user, meaning that a non displayed or hidden element will not be clickable. You can work around this problem by either bypassing that rule with a Javascript executor like so :

((IJavaScriptExecutor)Browser.Driver).ExecuteScript("arguments[0].click();", _element);

Where _element is the webElement you want to interact with

Or by using wait conditions.

More info here : Selenium Web Driver & Java. Element is not clickable at point (36, 72). Other element would receive the click:

Pierre Baran
  • 160
  • 10