2
<div class="wrapper"> <button class="w-full h-14 pt-2 pb-1 px-3 bg-accent text-dark-1 rounded-full md:rounded select-none cursor-pointer md:hover:shadow-big focus:outline-none md:focus:bg-accent-2 md:focus:shadow-small "> <div class="font-medium"> <div class="text-17 md:text-18 md:font-bold leading-18">Enter</div> <div class="text-13 md:text-12 font-normal md:font-medium leading-normal">2 hours</div> </div> </button> </div> 

So I'm trying to click this button but it has a huge class name in CSS. One of the ways possible is to use 'driver.find_element_by_css_selector' but i'm not sure if I am doing it right? I'd prefer an approach where I don't have to use the 'css_selector'. But if that is the only way I guess that'll have to do.

I tried this, but it did not seem to work:

self.driver.find_element_by_css_selector('.w-full h-14 pt-2 pb-1 px-3 bg-accent text-dark-1 rounded-full md:rounded select-none cursor-pointer md:hover:shadow-big focus:outline-none md:focus:bg-accent-2 md:focus:shadow-small ') 

Any suggestions? Thank you.

    2 Answers 2

    0

    Try this

    btn = self.driver.find_element_by_css_selector('.w-full h-14 pt-2 pb-1 px-3 bg-accent text-dark-1 rounded-full md:rounded select-none cursor-pointer md:hover:shadow-big focus:outline-none md:focus:bg-accent-2 md:focus:shadow-small ') btn.click() 
      0

      Use following locator to identify the element.

      Css selector :

      self.driver.find_element_by_css_selector("div.wrapper >button:nth-of-type(1)").click() 

      Xpath :

      self.driver.find_element_by_xpath("//div[@class='wrapper']/button[1]").click() 

      Ideally you should use WebDriverWait() and wait for element clickable

      WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.wrapper >button:nth-of-type(1)"))).click() 

      You need to import below libraries.

       from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC 
      3
      • For the CSS Selector that you suggested, i'm getting a 'selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"div.wrapper >button:nth-of-type(1)"}' @KunduK
        – amichow
        CommentedFeb 23, 2021 at 18:59
      • the webdriverwait gives a Timeout error because its not able to locate it
        – amichow
        CommentedFeb 23, 2021 at 19:01
      • Well. Try to wait more. And try to run test in debug mode or with something about 5 min sleep before interacting with the problem-element and check that you can find the locator from your test on the page in browser element console, if you can't find - try to find the locator that works and use it in your test.
        – gore
        CommentedFeb 25, 2021 at 4:49

      Start asking to get answers

      Find the answer to your question by asking.

      Ask question

      Explore related questions

      See similar questions with these tags.