简体   繁体   English

使用Selenium选择一个输入元素

[英]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. 我不知道如何到达按钮,这是非常基本的东西,但我没有找到任何好的例子。

This will click on the login button on moodle.tau.ac.il page. 这将点击moodle.tau.ac.il页面上的登录按钮。 The line driver.find_element_by_xpath(".//*[@id='login']/div/input").click() finds the login button on the page and clicks it. driver.find_element_by_xpath(".//*[@id='login']/div/input").click()在页面上找到登录按钮并单击它。 Xpath is just a selector type that you can use with selenium to find web elements on a page. Xpath只是一种选择器类型,您可以使用selenium在页面上查找Web元素。 You can also use ID, classname, and CSSselectors. 您还可以使用ID,classname和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()

The page has two identical login forms and your XPath returns the hidden one. 该页面有两个相同的登录表单,XPath返回隐藏的表单。 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: 或者使用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()

You could find it using XPath as mentioned by @ChrisP 您可以使用@ChrisP提到的XPath找到它

You could find it by CSS selector: "#login input[type='text']" 您可以通过CSS选择器找到它:“#login input [type ='text']”

Or you could also just submit the form... loginForm.submit() 或者您也可以只提交表单... loginForm.submit()

Ideally, you'd have a unique id for that button which would make it very easy to find. 理想情况下,您可以使用该按钮的唯一ID,这样可以很容易地找到它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM