Dahlin
Dahlin

Reputation: 177

Java Selenium get div element InvalidSelectorException

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 class="fixed bottom-5 right-5 cursor-pointer text-white sm:right-10">
<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">Yes</div>
</div>

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

Upvotes: 0

Views: 52

Answers (1)

Nikhil Sawant
Nikhil Sawant

Reputation: 377

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"));
    

Upvotes: 1

Related Questions