Selenium with Python

Selenium with Python中文翻译文档

Selenium是一个用于Web应用程序测试的工具。Selenium测试直接运行在浏览器中,就像真正的用户在操作一样。

from selenium import webdriver
from selenium.webdriver.common.keys import Keys


#启动phantomjs驱动
driver=webdriverPhantomJS(executable_path="D:/softwarepath/phantomjs-2.1.1-windows/bin/phantomjs.exe")

driver.get("http://www.baidu.com")#连接url,打开一个页面

"""
查找元素
"""
element=driver.find_element_by_id("passwd_id")
element = driver.find_element_by_name("passwd")
element = driver.find_element_by_xpath("//input[@id='passwd-id']")



"""
模拟键盘输入
"""

element.clear()#清空文本框内容
element.send_keys("some text")#向文本框输入文本
element.send_keys("and some",Keys.ARROW_DOWN)##模拟方向键

"""
操作cookies
"""
cookie={'name':'foo','value':'bar'}
driver.add_cookie(cookie)#添加cookie
cookies=driver.get_cookies()#获取当前URL的cookies

 
 






猜你喜欢

转载自blog.csdn.net/m0_37895939/article/details/80117995