标签:selenimu
WebDriver中常用方法:
clear() 清除文本
send_keys(*value) 模拟按键输入
click() 单机元素
网站的登陆
from selenium import webdriver from time import * driver = webdriver.Chrome() url = "https://login.huawei.com/login/?redirect=http%3A%2F%2Fw3.huawei.com%2Fnext%2Findexa.html" print(‘new access %s‘ %(url)) driver.get(url) driver.find_element_by_id(‘uid‘).clear() driver.find_element_by_id(‘uid‘).send_keys("c00358145") sleep(3) driver.find_element_by_id(‘password‘).clear() driver.find_element_by_id(‘password‘).send_keys("&CTT1106648034") driver.find_element_by_class_name(‘btn‘).click() sleep(3) driver.quit() 2.WebElement接口常用的方法 submit()方法用于提交表单 from selenium import webdriver from time import * driver = webdriver.Chrome() url = "https://www.baidu.com" print(‘new access %s‘ %(url)) driver.get(url) driver.find_element_by_id(‘kw‘).clear() driver.find_element_by_id(‘kw‘).send_keys("c00358145") sleep(3) driver.find_element_by_id(‘kw‘).submit() sleep(3) driver.quit()
定位搜索框通过submit提交搜素框的内容,达到点击搜素按钮的效果,有时候可以同click()函数交换使用
方法:size() 返回元素的尺寸
text() 获取元素的文本
get_attribute(name)获取属性的值
is_displayed() 设置该元素首付用户可见
from selenium import webdriver
from time import *
driver = webdriver.Chrome()
url = "https://www.baidu.com"
print(‘new access %s‘ %(url))
driver.get(url)
size = driver.find_element_by_id(‘kw‘).size
print(‘size%s‘ %size)
text = driver.find_element_by_xpath("//*[@id=‘u1‘]/a[1]").text
print(‘text%s‘ %text)
attribute = driver.find_element_by_id(‘kw‘).get_attribute(‘type‘)
print(‘%s‘ %attribute)
sleep(3)
driver.quit()
标签:selenimu
原文地址:http://9603083.blog.51cto.com/9593083/1869239