-1

I am trying to find this button and click on it. But I get this error:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".sqdOP  L3NKy   y3zKF     "}
  (Session info: chrome=87.0.4280.88)

I have noticed that it is looking for a different string '.sqdOP L3NKy y3zKF' bc it adds this point in front of the class name. Is this the problem?

<button class="sqdOP  L3NKy   y3zKF     " type="button">Follow</button>

Thank you!

user4248
  • 1
  • 1

2 Answers2

2

You can also locate it with this if there isn't a button with the same class name: driver.find_element_by_class_name("sqdOP")

Selenium doesn't recognize spaces in class names (unless you do something like what Villa_7 said) because those are actually "compound classes" (see this post and this one). It has something to do with CSS, I believe.

Fyi, it'd be helpful to see code for how you're currently trying to select the button.

coniferous
  • 67
  • 8
1

If classname value contains spaces, Selenium cannot locate it via dot "." You have to use this construction:

"[class='sqdOP  L3NKy   y3zKF     ']"

Or just try to locate by visible text using XPath:

"//button[text()='Follow']"

If you're getting Element click intercepted exception, just try to click via JS, like this:

 public void executeClickJS(WebDriver driver, WebElement webElement) {
    ((JavascriptExecutor) driver).executeScript("arguments[0].click();", webElement);
  }
Villa_7
  • 508
  • 4
  • 14
  • When I try to run the first solution I get this error: selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: An invalid or illegal selector was specified – user4248 Jan 07 '21 at 09:35
  • And the second solution gives another error: selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element is not clickable at point (315, 202). Other element would receive the click: (Session info: chrome=87.0.4280.88) – user4248 Jan 07 '21 at 09:39
  • @user4248 try to use 2nd solution but click via JavascriptExecutor. See my edited answer above. – Villa_7 Jan 08 '21 at 10:37