[英]Select an input element using Selenium
我試圖點擊這個按鈕移動到登錄頁面。 我的代碼是:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('http://moodle.tau.ac.il/')
這很好但我只能通過使用找到表格
loginform = driver.find_element_by_xpath("//form[@id='login']/")
我不知道如何到達按鈕,這是非常基本的東西,但我沒有找到任何好的例子。
這將點擊moodle.tau.ac.il頁面上的登錄按鈕。 行driver.find_element_by_xpath(".//*[@id='login']/div/input").click()
在頁面上找到登錄按鈕並單擊它。 Xpath只是一種選擇器類型,您可以使用selenium在頁面上查找Web元素。 您還可以使用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()
該頁面有兩個相同的登錄表單,XPath返回隱藏的表單。 所以有了可見的:
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()
或者使用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()
您可以使用@ChrisP提到的XPath找到它
您可以通過CSS選擇器找到它:“#login input [type ='text']”
或者您也可以只提交表單... loginForm.submit()
理想情況下,您可以使用該按鈕的唯一ID,這樣可以很容易地找到它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.