使用javascript操作页面元素
#encoding=utf-8
from selenium import webdriver
from selenium.common.exceptions import WebDriverException
import unittest
import traceback
import time
class TestDemo(unittest.TestCase):
def setUp(self):
self.driver=webdriver.Firefox(executable_path="f:\\geckodriver")
def test_executeScript(self):
url="http://www.baidu.com"
self.driver.get(url)
searchInputBoxJS="document.getElementById('kw').value='光荣之路';"
searchButtonJS="document.getElementById('su').click()"
try:
self.driver.execute_script(searchInputBoxJS)
time.sleep(2)
self.driver.execute_script(searchButtonJS)
time.sleep(2)
self.assertTrue("百度百科"in self.driver.page_source)
except WebDriverException as e:
print("在页面没有找到要操作的页面元素",traceback.print_exc())
except AssertionError as e:
print("页面不存在断言的关键字串")
except Exception as e:
print (traceback.print_exc())
def tearDown(self):
self.driver.quit()
if __name__=='__main__':
unittest.main()
调用javascript方法操作WEB界面滚动条
#encoding=utf-8
from selenium import webdriver
import unittest
import traceback
import time
class TestDemo(unittest.TestCase):
def setUp(self):
self.driver=webdriver.Firefox(executable_path="f:\\geckodriver")
def test_scroll(self):
url="http://www.seleniumhg.org"
try:
self.driver.get(url)
#使用Javascript的srcollTo函数和document.body.scrollHeight参数
#将滚动条拖到最下方
self.driver.execute_script("windows.scrollTo(100,document.body.scrollHeight);")
time.sleep(3)
#使用scrollBy方法,使用0和400横纵坐标参数,纵向拖动400像素
self.driver.execute_script("document.getElementById('chioe').scrollIntoView(true);")
time.sleep(3)
self.driver.execute_script("windows.scrollBy(0,400);")
time.sleep(3)
except Exception as e:
print(traceback.print_exc())
def tearDown(self):
self.driver.quit()
if __name__=='__main__':
unittest.main()
**
在Ajax方式产生的浮动框中,单击选择包含某个关键字的选项
**
第一种方法:通过模拟键盘下的箭头进行选择悬浮框选项
#encoding=utf-8
from selenium import webdriver
import unittest
from selenium.webdriver.common.keys import Keys
import time
class TestDemo(unittest.TestCase):
def setUp(self):
self.driver=webdriver.Firefox(executable_path="f:\\geckodriver")
def test_AjaxDivOptionByKeys(self):
url="http://www.sogou.com/"
self.driver.get(url)
searchBox=self.driver.find_element_by_id("query")
searchBox.send_keys("光荣之路")
time.sleep(2)
#选择悬浮框中的第几个联想关键词就循环几次
for i in range(3):
#模拟键盘单击下箭头
searchBox.send_keys(Keys.DOWN)
time.sleep(0.5)
#选择后回车
searchBox.send_keys(Keys.ENTER)
time.sleep(3)
def tearDown(self):
self.driver.quit()
if __name__=='__main__':
unittest.main()
第二种方法:通过匹配模糊内容选择悬浮框中选项
#encoding=utf-8
from selenium import webdriver
import unittest
from selenium.common.exceptions import NoSuchElementException
import traceback
import time
class TestDemo(unittest.TestCase):
def setUp(self):
self.driver=webdriver.Firefox(executable_path="f:\\geckodriver")
def test_AjaxDivOptionByWords(self):
url="http://www.sogou.com/"
self.driver.get(url)
try:
searchBox=self.driver.find_element_by_id("query")
searchBox.send_keys("光荣之路")
time.sleep(2)
#查找内容包含“篮球电影”的悬浮选项
suggetion_option=self.driver.find_element_by_xpath("//ul/li[contains(.,'电影')]")
suggetion_option.click()
time.sleep(3)
except NoSuchElementException as e:
print(traceback.print_exc())
def tearDown(self):
self.driver.quit()
if __name__=='__main__':
unittest.main()
结束windows中浏览器的进程
#encoding=utf-8
from selenium import webdriver
import unittest
class TestDemo(unittest.TestCase):
#def setUp(self):
#self.driver=webdriver.Firefox(executable_path="f:\\geckodriver")
def test_killWinsowsProcess(self):
firefoxDriver=webdriver.Firefox(executable_path="f:\\geckodriver")
# ieDriver=webdriver.Ie(executable_path="f:\\IEDriverServer")
chromeDriver=webdriver.Chrome(executable_path="f:\\chromedriver")
import os
returnCode=os.system("taskkill/F/iM firefox.exe")
if returnCode==0:
print("成功杀掉firefox进程")
else:
print("结束进程失败")
#returnCode = os.system("taskkill/F/iM iexplore.exe")
'''
if returnCode == 0:
print("kill ie")
else:
print("ie")
'''
returnCode=os.system("taskkill/F/iM chrome.exe")
if returnCode==0:
print("kill chrome")
else:
print("chrome")
#def tearDown(self):
#self.driver.quit()
if __name__=='__main__':
unittest.main()
修改页面元素的属性
html文件:
<html>
<meta http-equiv="Content-Type"content="text/html;charset=utf-8"/>
<head>
<title>设置文本属性</title>
</head>
<body>
<input type="text" id="text" value="今年夏天西瓜特别甜" size=100>
文本框
</input>
</body>
</html>
实例代码:
#encoding=utf-8
from selenium import webdriver
import unittest
def addAttribute(driver,elementObj,attributeName,value):
driver.execute_script("arguments[0].%s=arguments[1]"%attributeName,elementObj,value)
def setAttribute(driver,elementObj,attributeName,value):
driver.execute_script("arguments[0].setAttribute(arguments[1],arguments[2])",elementObj,attributeName,value)
def getAttribute(elementObj,attributeName):
return elementObj.get_attribute(attributeName)
def removeAttribute(driver,elementObj,attributeName):
driver.execute_script("arguments[0].removeAttribute(arguments[1])",elementObj,attributeName)
class TestDemo(unittest.TestCase):
def setUp(self):
self.driver=webdriver.Firefox(executable_path="f://geckodriver")
def test_dataPicker(self):
url="file:///F:/17.html"
self.driver.get(url)
element=self.driver.find_element_by_xpath("//input")
addAttribute(self.driver,element,"name","search")
print('添加的新属性值%s="%s"'%("name",getAttribute(element,"name")))
print("更改文本框中的内容前的内容:",getAttribute(element,"value"))
setAttribute(self.driver,element,"value","这是更改后的文字内容")
print("更改文本框中内容后的内容:",getAttribute(element,"value"))
print("更改前size属性值:",getAttribute(element,"size"))
setAttribute(self.driver,element,"size",20)
print("更改后文本标签中的size属性值:",getAttribute(element,"size"))
print("文本框value属性值:",getAttribute(element,"value"))
removeAttribute(self.driver,element,"value")
print("删除value属性值后value的属性值:",getAttribute(element,"value"))
def tearDown(self):
self.driver.quit()
if __name__ == '__main__':
unittest.main()
人工无干预下载某个文件
#encoding=utf-8
from selenium import webdriver
import unittest
import time
class TestDemo(unittest.TestCase):
def setUp(self):
profile=webdriver.FirefoxProfile()
profile.set_preference('browser.download.dir','d:\\iDownload')
profile.set_preference('browser.download.folderList',2)
profile.set_preference('browser.helperApps.alwaysAsk.force',False)
profile.set_preference('browser.download.manager.showWhenSrarting',False)
profile.set_preference("browser.download.manager.focusWhenStarting",False)
profile.set_preference("browser.download.manager.alertOnEXEOpen",False)
profile.set_preference("browser.helperApps.neverAsk.openFile","applicat/pdf")
profile.set_preference("browser.helperApps.neverAsk.saveToDisk",'application/zip,application/octet-stream')
profile.set_preference("browser.dowmload.manager.closeWhenDone",False)
self.driver=webdriver.Firefox(executable_path="f:\\geckodriver",firefox_profile=profile)
def test_dataPicker(self):
url1="https://github.com/mozilla/geckodriver/releases"
self.driver.get(url1)
self.driver.find_element_by_xpath( './/*[@href="/mozilla/geckodriver/releases/download/v0.24.0/geckodriver-v0.24.0-win64.zip"]').click()
time.sleep(10)
url1="https://www.python.org/download/release/Python-2712/"
self.driver.get(url1)
self.driver.find_element_by_link_text("windows x86-64 MSI installer").click()
time.sleep(100)
def tearDown(self):
self.driver.quit()
if __name__=='__main__':
unittest.main()