jbendy
jbendy

Reputation: 93

Python + Selenium - Cannot locate element by class name

I'm attempting to use Selenium with Python to sign in to a webpage. I've gotten to the point where I can successfully the username and password. Initially I tried just using the "Enter" key from the password field like so -

passwordField = browser.find_element_by_css_selector('#password')
passwordField.send_keys(Keys.ENTER)

This doesn't work for me, even though pressing "Enter" while my cursor is in the password field signs me in just fine.

 

So now I'm trying to click the sign in button, but my problem is that I can't seem to locate the element properly.

In the Chrome console, the button is listed as such (new lines added for readability) -

<button class="btn__primary--large from__button--floating mercado-button--primary"
data-litms-control-urn="login-submit"
type="submit" aria-label="Sign in">Sign in</button>

The class name contains spaces, so I attempted this -

signInButton = browser.find_element_by_class_name('btn__primary--large from__button--floating mercado-button--primary')
signInButton.click()

Unfortunately Selenium can't find that element -

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".btn__primary--large from__button--floating mercado-button--primary"}

How can I target this sign in button?

 

Edit: here's the web address - https://www.linkedin.com/login?fromSignIn=true&trk=guest_homepage-basic_nav-header-signin

Upvotes: 1

Views: 172

Answers (2)

Arundeep Chohan
Arundeep Chohan

Reputation: 9969

Try using the following to get the element by class and clicking it.

 driver.find_element_by_class_name("mercado-button--primary").click()

Upvotes: 0

Rob T
Rob T

Reputation: 74

Apparently it tries to treat the whole string as one class however these are multiple classes. Have you tried with passing in only one class name? Although it is possible that it would find multiple items. Probably you'd be better of looking for an #id instead?

Upvotes: 1

Related Questions