Home > Software design >  Find elements by multiple class names in Selenium Java
Find elements by multiple class names in Selenium Java

Time:10-11

I am trying automate one function on Amazon's site at enter image description here

My question is, why am I getting an empty list and not the list with all of the elements of the div that is selected?

CodePudding user response:

Only one class name can be used with the By.className(String) method. From the JavaDoc:

Find elements based on the value of the "class" attribute. Only one class name should be used. If an element has multiple classes, please use cssSelector(String).

You can use the By.cssSelector(String) method instead:

driver.findElements(By.cssSelector("div[class='a-box-inner a-padding-extra-large']"));

CodePudding user response:

There are 2 possible problems here:

  1. You are possibly missing a delay.
  2. The locator of element you trying to collect contains multiple class name values. To locate them you need to use CSS Selector or XPath, not By.className.
    So, instead of
List<WebElement> e = BaseTest.driver.findElements(By.className("a-box-inner a-padding-extra-large"));

Try using this:

WebDriverWait wait = new WebDriverWait(BaseTest.driver, 20);
List<WebElement> e = wait.until(ExpectedConditions.visibilityOfAllElements(By.cssSelector("div.a-box-inner.a-padding-extra-large")));

Or this:

WebDriverWait wait = new WebDriverWait(BaseTest.driver, 20);
List<WebElement> e = wait.until(ExpectedConditions.visibilityOfAllElements(By.xpath("//div[@class='a-box-inner a-padding-extra-large']")));
  • Related