表单操作

文本框操作

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

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

This can also be used to set file inputs.

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

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

# -*- coding:utf-8 -*- from selenium import webdriver import os chromedriver = "./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()

下拉列表

# -*- coding:utf-8 -*- from selenium import webdriver from time import sleep chromedriver = "./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) # seconds elem = driver.find_element_by_xpath('//*[@id="form"]//li[2]') elem.click()

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

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

<select name="province" class="ui-select-small" id="yui_3_16_0_1_1472275682924_335">
    <option value="-1">--省--</option>
    <option value="110000" selected="selected">北京</option>
    <option value="120000">天津</option>
    <option value="130000">河北</option>
    <option value="140000">山西</option>
    ....
</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 Select from selenium import webdriver from time import sleep chromedriver = "./chromedriver" driver = webdriver.Chrome(executable_path=chromedriver) driver.get('http://www.meituan.com/index/changecity/initiative') s1 = Select(driver.find_element_by_name('province')) # 实例化Select print 'start watings 5s' sleep(5) # seconds print 'select_by_index' s1.select_by_index(2) # 选择第二项选项 sleep(5) # seconds print 'select_by_value' s1.select_by_value("130000") # 选择value="130000"的项 sleep(5) # seconds print '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 Select from selenium import webdriver from time import sleep chromedriver = "./chromedriver" driver = webdriver.Chrome(executable_path=chromedriver) driver.get('http://www.meituan.com/index/changecity/initiative') #Ajax未加载完成,需要sleep sleep(4) s1 = Select(driver.find_element_by_name('province')) # 实例化Select s1.select_by_visible_text("山西") #s1.select_by_index(4) sleep(4) # seconds s2 = Select(driver.find_element_by_name('city')) # 实例化Select s2.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()