1. 控制窗口大小
form selenium import webdriver
driver = webdriver.Chrome()
driver.get("http://www.baidu.com")
print("设置浏览器宽480,高800显示")
driver.set_window_size(480,800)
driver.quit()
2. 控制浏览器前进,后退
form selenium import webdriver
frist_url = "http://www.baidu.com"
print("frist is %s" %(frist_url))
driver.get(frist_url)
second_url = "http://www.sina.com"
print("second is %s" %(second_url))
driver.get(second_url)
print("back to frist_url %s" %(frist_url))
driver.back()
print("forward to second_url %s" %(second_url))
driver.forward()
driver.quit()
3. 浏览器刷新
driver.refresh()
4. 简单元素的操作(sina邮箱登录)
form selenium import webdrver
driver = webdriver.Chrome()
driver.get("http://mail.sina.com")
driver.find_element_by_id("freename").clear()
driver.find_element_by_id("freename").send_keys("[email protected]")
time.sleep(2)
driver.find_element_by_id("freepassword").clear()
driver.find_element_by_id("freepassword").send_keys("")
time.sleep(2)
driver.find_element_by_class_name("loginBtn").click()
time.sleep(4)
driver.quit()
5. 提交表单
.click()可以代替.submit()
6. 获得输入框尺寸
size = driver.find_element_by_id("kw").size
print(size)
7. 获得元素的文字
text = driver.find_element_by_id("cp").text
print(text)
8. 返回元素的属性值
attribute = driver.find_element_by_id("kw").get_attribute("type")
print(attribute)
9. 返回元素是否可见
result = driver.find.element_by_id("kw").displayed()
print(result)
10. 输入文字和清空文字
browser.find_element_by_id(“kw”).send_keys(“1234”) 在kw元素上输入‘1234’
browser.find_element_by_id(“kw”).clear() 清空kw元素输入的信息