0

I am trying to figure out how to use Selenium with div class:

<div class="main-button ol-has-tooltip jse-info">

tried everything to find this element and click on it:

driver.findElement(By.xpath("/html/body/div[1]/div[3]/div[2]/div[1]/div[2]")).click();

and :

driver.findElement(By.cssSelector("main-button ol-has-tooltip jse-info")).click();

But did not work for me, How can I handle this ? Thank you.

DINA TAKLIT
  • 7,074
  • 10
  • 69
  • 74
Stan R
  • 11
  • 1
  • 1
    We don't have enough context to know whether the first locator is correct but it's a copy/paste XPath which is extremely brittle. Anything that starts at the html tag or has more than a few levels or that has indices in them is generally a bad and brittle locator. The CSS selector you have is not actually formatted correctly. A `.` indicates a class where a space indicates a descendant. You want `div.main-button.ol-has-tooltip.jse-info`. Try that and see if it works. If it doesn't, add a wait and make sure the element isn't in an `IFRAME`. – JeffC May 07 '19 at 04:12

3 Answers3

0

Welcome to SO.

If you are trying to click on a div with main-button ol-has-tooltip jse-info clas name then here are right locator strategies using xpath and css.

Xpath General notation `//tagName[@attribute='attributeVale']

//div[@class='main-button ol-has-tooltip jse-info']

driver.findElement(By.xpath("//div[@class='main-button ol-has-tooltip jse-info']")).click();

CSS: General notation tagName.class.name.replace.space.with.dot

div.main-button.ol-has-tooltip.jse-info

driver.findElement(By.cssSelector("div.main-button.ol-has-tooltip.jse-info")).click();

You can always test your xpath and CSS in the browser dev tools. Refer to my answer how to check xpath.

supputuri
  • 13,644
  • 2
  • 21
  • 39
0

It is possible that class is used in multiple tag

So it is better to used XPath and CSS as said by @supputuri

You can also be used the elementToBeClickable wait condition here

WebElement element = driver.findElement(By.cssSelector("div.main-button.ol-has-tooltip.jse-info"));
WebDriverWait wait = new WebDriverWait(driver, 10); 
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(element));
element.click();
iamsankalp89
  • 4,607
  • 2
  • 15
  • 36
  • I tried it with no luck: Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: Element
    ...
    is not clickable at point (39, 37). Other element would receive the click:
    ...
    (Session info: chrome=74.0.3729.131)
    – Stan R May 07 '19 at 23:42
-1

Please check if the div is another frame. In the case it is in another frame, you may have to use the driver.switchTo().frame(""). Once you are done with the operations in the frame you must use driver.switchTo().defaultContent(). Secondly check if the div element is clickable. It would be easier to suggest a solution once you add the exception details if you get any.