表单操作

文本框操作

根据示例:了解通过send_keys向文本框中输入文字

  1. form_textfield = driver.find_element_by_name('username')
  2. form_textfield.send_keys("admin")

This can also be used to set file inputs.

  1. file_input = driver.find_element_by_name('profilePic')
  2. file_input.send_keys("path/to/profilepic.gif")

示例: http://www.chuantu.biz/

-- coding:utf-8 --from selenium import webdriverimport oschromedriver = "./chromedriver"driver = webdriver.Chrome(executable_path=chromedriver)driver.get("http://www.chuantu.biz/")upload = driver.find_element_by_name('uploadimg')upload.send_keys(os.path.abspath("/home/python/project/dzjg_big1.png"))print 'end'#driver.quit()

下拉列表

表单操作 - 图1

-- coding:utf-8 --from selenium import webdriverfrom time import sleepchromedriver = "./chromedriver"driver = webdriver.Chrome(executable_path=chromedriver)print 'start'driver.get("http://www.baidu.com/")elem = driver.find_element_by_name("wd")elem.send_keys("d")sleep(3) # secondselem = driver.find_element_by_xpath('//*[@id="form"]//li[2]')elem.click()

第二个实例:http://www.meituan.com/index/changecity/initiative

针对于网站中的省份select框:

  1. <select name="province" class="ui-select-small" id="yui_3_16_0_1_1472275682924_335">
  2. <option value="-1">--省--</option>
  3. <option value="110000" selected="selected">北京</option>
  4. <option value="120000">天津</option>
  5. <option value="130000">河北</option>
  6. <option value="140000">山西</option>
  7. ....
  8. </select>

WebDriver中提供了一个叫Select的方法,可以帮助我们完成这些事情。首先获取了select元素,也就是下拉选项卡,将元素实例化为Select类对象。

Select类提供了三种选择某一选项的方法:

select_by_index(index)
select_by_value(value)
select_by_visible_text(text)

-- coding:utf-8 --from selenium.webdriver.support.ui import Selectfrom selenium import webdriverfrom time import sleepchromedriver = "./chromedriver"driver = webdriver.Chrome(executable_path=chromedriver)driver.get('http://www.meituan.com/index/changecity/initiative')s1 = Select(driver.find_element_by_name('province')) # 实例化Selectprint 'start watings 5s'sleep(5) # secondsprint 'select_by_index's1.select_by_index(2) # 选择第二项选项sleep(5) # secondsprint 'select_by_value's1.select_by_value("130000") # 选择value="130000"的项sleep(5) # secondsprint 'select_by_visible_text's1.select_by_visible_text("山西") # 选择text="山西"的值,即在下拉时我们可以看到的文本print 'end'

以上是三种选择下拉框的方式,注意:index从0开始value是option标签的一个属性值,并不是显示在下拉框中的值visible_text是在option标签中间的值,是显示在下拉框的值####表单提交如果你把表单都填好了,最后肯定要提交表单

-- coding:utf-8 --from selenium.webdriver.support.ui import Selectfrom selenium import webdriverfrom time import sleepchromedriver = "./chromedriver"driver = webdriver.Chrome(executable_path=chromedriver)driver.get('http://www.meituan.com/index/changecity/initiative')#Ajax未加载完成,需要sleepsleep(4)s1 = Select(driver.find_element_by_name('province')) # 实例化Selects1.select_by_visible_text("山西")#s1.select_by_index(4)sleep(4) # secondss2 = Select(driver.find_element_by_name('city')) # 实例化Selects2.select_by_visible_text("大同")sleep(4) # seconds#三种方式#是个坑,容性问题#driver.find_element_by_class_name('btn btn-mini').submit()#driver.find_element_by_id("changeCity").submit()#driver.find_element_by_xpath('//[@value="确定"]').submit()driver.find_element_by_xpath('//[@value="确定"]').click()