表單操作

text

模擬輸入值到元素中

<form action="/action_page.php">
  <label for="username">Username:</label>
  <input type="text" id="username" name="username" /><br />
  <label for="pwd">Password:</label>
  <input type="password" id="pwd" name="pwd" /><br /><br />
  <input type="submit" value="Submit" />
</form>
form_textfield = driver.find_element_by_name('username')
form_textfield.send_keys("admin")

radio

<input type="radio" id="male" name="gender" value="male" />
<label for="male">Male</label><br />
<input type="radio" id="female" name="gender" value="female" />
<label for="female">Female</label><br />
<input type="radio" id="other" name="gender" value="other" />
<label for="other">Other</label>
male_radio = driver.find_element_by_css_selector("[for='male']")
male_radio.click()

checkbox

<input type="checkbox" id="vehicle1" name="vehicle1" value="Bike" />
<label for="vehicle1"> I have a bike</label><br />
<input type="checkbox" id="vehicle2" name="vehicle2" value="Car" />
<label for="vehicle2"> I have a car</label><br />
<input type="checkbox" id="vehicle3" name="vehicle3" value="Boat" />
<label for="vehicle3"> I have a boat</label><br />
male_radio = driver.find_element_by_css_selector("[for='vehicle1']")
male_radio.click()

textarea

<label for="review">Review:</label>

<textarea id="review" name="review" rows="4" cols="50">
123
</textarea>
textarea = driver.find_element_by_name('review')
textarea.send_keys("demo")

submit

模擬 submit 行為

driver.find_element_by_id("submit").click()
element.submit()

上傳檔案

picture = driver.find_element_by_css_selector("[type='file']")
picture.send_keys('/demo/screenshot.png')

select

<select class="month-select">
  <option value="0">January</option>
  <option value="1">February</option>
  <option value="2">March</option>
  <option value="3">April</option>
  <option value="4">May</option>
  <option value="5">June</option>
  <option value="6">July</option>
  <option value="7">August</option>
  <option value="8">September</option>
  <option value="9">October</option>
  <option value="10">November</option>
  <option value="11">December</option>
</select>
from selenium.webdriver.support.ui import Select
select_year = Select(driver.find_element_by_css_selector('.month-select'))
select_year.select_by_value('1')
select = Select(driver.find_element_by_id('id'))

select.select_by_index(index)
select.select_by_visible_text("text")
select.select_by_value(value)

options = select.options

select.deselect_all()

參考文獻

results matching ""

    No results matching ""