8

Inspect

Im trying to click on this button to move to the login page. my code is :

from selenium import webdriver
driver = webdriver.Chrome()
driver.get('http://moodle.tau.ac.il/')

thats work fine but i can only find the form by using

loginform = driver.find_element_by_xpath("//form[@id='login']/")

I don't know how to get to the button, it's very basic stuff but I didn't find any good example.

1
  • Why can't you just extend your xpath expression to select the input? i.e. //form[@id="login"]/div/input. Commented Apr 21, 2016 at 19:30

3 Answers 3

6

This will click on the login button on moodle.tau.ac.il page. The line driver.find_element_by_xpath(".//*[@id='login']/div/input").click() finds the login button on the page and clicks it. Xpath is just a selector type that you can use with selenium to find web elements on a page. You can also use ID, classname, and CSSselectors.

from selenium import webdriver

driver = new webdriver.Chrome()

driver.get('moodle.tau.ac.il')
# This will take you to the login page.
driver.find_element_by_xpath(".//*[@id='login']/div/input").click()

# Fills out the login page
elem = driver.find_element_by_xpath("html/body/form/table/tbody/tr[2]/td/table/tbody/tr[1]/td[2]/input")
elem.send_keys('Your Username')
elem = driver.find_element_by_xpath("html/body/form/table/tbody/tr[2]/td/table/tbody/tr[3]/td[2]/input")
elem.send_keys('Your ID Number')
elem = driver.find_element_by_xpath("html/body/form/table/tbody/tr[2]/td/table/tbody/tr[1]/td[2]/input")
elem.send_keys('Your Password')
driver.find_element_by_xpath("html/body/form/table/tbody/tr[2]/td/table/tbody/tr[7]/td[2]/input").click()
Sign up to request clarification or add additional context in comments.

1 Comment

Your way doesnt work, it doesnt able to locate the elements
2

The page has two identical login forms and your XPath returns the hidden one. So with the visible one:

from selenium import webdriver

driver = webdriver.Chrome()

driver.get(r"http://moodle.tau.ac.il/")
driver.find_element_by_css_selector("#page-content #login input[type=submit]").click()

Or with an XPath:

from selenium import webdriver

driver = webdriver.Chrome()

driver.get(r"http://moodle.tau.ac.il/")
driver.find_element_by_xpath("id('page-content')//form[@id='login']//input[@type='submit']").click()

Comments

-2

You could find it using XPath as mentioned by @ChrisP

You could find it by CSS selector: "#login input[type='text']"

Or you could also just submit the form... loginForm.submit()

Ideally, you'd have a unique id for that button which would make it very easy to find.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.