Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select an input element using Selenium

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.

like image 875
Paz Avatar asked Apr 21 '16 19:04

Paz


People also ask

Can we click input tag in Selenium?

We can click on a button with <input type= file> across browsers with Selenium webdriver. First of all we need to identify the element with the help of locators like xpath or css, then apply sendKeys() method where the path of the file to be uploaded is passed.

How do I enter input in Selenium?

We can type Enter/Return key in Selenium. We shall use the sendKeys method and pass Keys. ENTER as an argument to the method. Also, we can use pass Keys.


2 Answers

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()
like image 99
Colby Avatar answered Sep 28 '22 19:09

Colby


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()
like image 34
Florent B. Avatar answered Sep 28 '22 20:09

Florent B.