0

The element I got is <div class="x-tool x-box-item" id="tool-123" src="data:image/gif;base64" class="new-img x-tool-maximize".

I particularly need this class="new-img x-tool-maximize" because its the common of all the screen.

I already tried

driver.findElement(By.className("new-img.x-tool-maximize")).click()

and

driver.findElement(By.className("new-img x-tool-maximize")).click();

and

driver.findElement(By.xpath("//div[contains(@class, 'value') and contains(@class, 'test')]"))``;
DebanjanB
  • 118,661
  • 30
  • 168
  • 217
  • Why does the div contain two class attributes? `class="x-tool x-box-item"` and `class="new-img x-tool-maximize"`? AFAIK `By.className` only accepts a single classname – Nitin Jul 21 '20 at 05:40

4 Answers4

1

You should be able to use a CSS selector to find this type of element. You’ll want something like driver.findElement(By.cssSelector("div.new-img.x-tool-maximize")).

JimEvans
  • 25,799
  • 6
  • 74
  • 104
0
You can use list in selenium if you have multiple elements with same locators 

  

     List<WebElement> classes=driver.findElements(By.classname("new-img x-tool-maximize));
// if you want click 1 st class name element use this following line 
     classes.get(0).click();

OR  // if you want click 2 nd  class name element use this following line 
     classes.get(1).click();
Justin Lambert
  • 839
  • 1
  • 7
  • 11
0

Use a list to get all the WebElements with specific classes

 List<WebElement> list = driver.findElements(By.cssSelector("div.new-img.x-tool-maximize"));
Ledjon
  • 25
  • 4
0

As you intent to click() on the element using only the className attribute values, ideally you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:

  • cssSelector using only the className attribute values:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector(".new-img.x-tool-maximize"))).click();
    

For a more canonicl approach you can indlude the tagName as follows:

  • cssSelector:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.new-img.x-tool-maximize]"))).click();
    

References

You can find a couple of relevant detailed discussions in:

DebanjanB
  • 118,661
  • 30
  • 168
  • 217