Daniils Koselevs
Daniils Koselevs

Reputation: 25

How to find element by its class Selenium?

I'm trying to click Log In button on Facebook Page. I'm trying to locate the button by class_name, but it returns an error. html: enter image description here

python:

submit   = browser.find_element_by_class_name("a8c37x1j ni8dbmo4 stjgntxs l9j0dhe7 ltmttdrg g0qnabr5").text("Log In")

Upvotes: 0

Views: 153

Answers (2)

HedgeHog
HedgeHog

Reputation: 25196

You can chain it with css selectors.

Example

from selenium import webdriver

html = '''<span class="a8c37x1j ni8dbmo4 stjgntxs l9j0dhe7 ltmttdrg g0qnabr5">Log In</span>'''

driver = webdriver.Chrome(executable_path=r'C:\Program Files\ChromeDriver\chromedriver.exe')
driver.get("data:text/html;charset=utf-8,{0}".format(html))

driver.find_element_by_css_selector("span.a8c37x1j.ni8dbmo4.stjgntxs.l9j0dhe7.ltmttdrg.g0qnabr5")

Upvotes: 1

Frederick
Frederick

Reputation: 470

The space in the class name is like a seperator for the classes in css, what means that you look for many classes at once, so it wont work. Try it like this:

submit   = browser.find_element_by_class_name("a8c37x1j")

And when you want to click the button later on, you have to remove .text, because it just gives you the text of the button, but not the element. In order to click, just do this:

submit.click()

Upvotes: 1

Related Questions