Home > Software design >  Java Selenium get div element InvalidSelectorException
Java Selenium get div element InvalidSelectorException

Time:02-05

I am trying to get a div contained on a html site. This div itself is clickable. I am using Selenium in Java for this. I am using a Chrome webdriver.

What I tried:

WebElement btn = driver.findElement(By.className(
            "flex h-10 w-10 items-center justify-center rounded-full border-4 border-white font-SFProBold text-base sm:h-16 sm:w-16 sm:text-xl"));

and

WebElement btn = driver.findElement(By.cssSelector(
            "flex h-10 w-10 items-center justify-center rounded-full border-4 border-white font-SFProBold text-base sm:h-16 sm:w-16 sm:text-xl"));

The first one gives me a InvalidSelectorException that Compound class names are not permitted, while the second one gives me a invalid selector: An invalid or illegal selector was specified error.

The Html Code is looking like this:

<div >
<div >Yes</div>
</div>

I am not sure if the classname is one class or if flex, h-10, w-10 are multiple classes.

CodePudding user response:

You are supplying incorrect information to By.className() as well as By.cssSelector() methods.

If you want to select element which has multiple classes then use following:

  • XPATH

    driver.findElement(By.xpath("//div[contains(@class, 'flex h-10 w-10 items-center justify-center rounded-full border-4 border-white font-SFProBold text-base sm:h-16 sm:w-16 sm:text-xl')]"));

  • CSS SELECTOR

    driver.findElement(By.cssSelector("div[class*='flex h-10 w-10 items-center justify-center rounded-full border-4 border-white font-SFProBold text-base sm:h-16 sm:w-16 sm:text-xl']"));

    driver.findElement(By.cssSelector("div.flex.h-10.w-10.items-center.justify-center.rounded-full.border-4.border-white.font-SFProBold.text-base.sm:h-16.sm:w-16.sm:text-xl"));

  • Related