0

Fairly new to selenium Python so please go easy.

For:

<button class="slds-button slds-show" aria-haspopup="dialog"><div class="slds-icon-waffle"><div class="slds-r1"></div><div class="slds-r2"></div><div class="slds-r3"></div><div class="slds-r4"></div><div class="slds-r5"></div><div class="slds-r6"></div><div class="slds-r7"></div><div class="slds-r8"></div><div class="slds-r9"></div><span class="slds-assistive-text">App Launcher</span></div></button>

I'm trying to click on the button by getting the class:

applauncher = driver.find_element(By.CLASS_NAME, 'slds-button slds-show')

which results in a NoSuchElementException. I've tried putting a . in the class name to remove the whitespace and "" instead of single quotes. I usually have a wait in there but removed it for debugging, the command doesn't work with it anyway.

The thing is finding it by XPATH works absolutely fine:

applauncher = WebDriverWait(driver, 10).until(EC.element_to_be_clickable(("xpath", "//button[@class='slds-button slds-show']")))

Even though I already have a solution, I'd like to improve my understanding and figure out why finding the element by class name doesn't work. Thanks.

  • 1
    Try `By.CSS_SELECTOR, 'button.slds-button.slds-show'` – user47 Aug 04 '22 at 16:07
  • Thanks but that also didn't work, same exception. – the_jez Aug 04 '22 at 16:13
  • A space between two names in a class means that element has two styles associated with it. ("slds-button" and "slds-show") When you try a By.CLASS_NAME method it'll search for "slds-button slds-show" as the style class name... which it won't find. The XPATH is explicitly checking an attribute so it matches. I believe checking by classname for either name will match, or use an AND for both names. – pcalkins Aug 04 '22 at 20:09
  • Also see this thread: https://stackoverflow.com/questions/21713280/find-div-element-by-multiple-class-names – pcalkins Aug 04 '22 at 20:13
  • Could you share the URL? Sometimes the elements are inside other iframes and hence, cannot be accessed before we switch to that iframe – babban babu Aug 05 '22 at 09:35
  • Thanks for that explanation, that makes sense. I've identified that both `slds-button` and `slds-show` are needed to uniquely identify the button. I tried using an and to force it to look for both, which doesn't work. After reviewing the other thread, it suggests the CSS_SELECTOR solution, as suggested by Firelord, so now I can't figure out why that doesn't work. – the_jez Aug 05 '22 at 09:40
  • I can't share the URL unfortunately as it's an internal Salesforce implementation. I'm going to search for iframes as I suspect Salesforce is full of them – the_jez Aug 05 '22 at 10:44

0 Answers0