软件测试爬虫,【松勤软件自动化测试】Python3-爬虫~selenium\phantomjs\ActionChains百度例子...
#安裝:pip install selenium=2.48.0
#顯示:pip show selenium
#卸載:pip uninstall selenium
#模擬用戶行為
importos,time
fromseleniumimportwebdriver,common
importselenium
fromselenium.webdriver.common.action_chains?importActionChains
#用selenium做自動化,有時候會遇到需要模擬鼠標操作才能進行的情況,
# 比如單擊、雙擊、點擊鼠標右鍵、拖拽等等。而selenium給我們提供了一個類來處理這類事件——ActionChains
#瀏覽器
driver=webdriver.PhantomJS()
#訪問
driver.get('https://www.baidu.com/')
#截屏root_dir='baidu'
if notos.path.exists(root_dir):
os.mkdir(root_dir)
file_name=root_dir+'/homepage.png'
driver.save_screenshot(file_name)
#模擬用戶行為操作
#輸入行為
timeout=4
count=0
while True:
try:
ifcount>4:
break
obj=driver.find_element_by_id('kw')
# print(obj)
#發送關鍵字
obj.send_keys(u'章子怡')#若出現編碼問題字符串前加"u",表示unicode碼
break
exceptcommon.exceptions.NoSuchAttributeException?ase:
print(e)
time.sleep(timeout)
count+=1
file_name=root_dir+'/zhangziyi.png'
driver.save_screenshot(file_name)
'''
try:
obj = driver.find_element_by_id("kw")
obj.send_keys(u"章子怡")
print(obj)
break
except common.exceptions.NoSuchElementException as e:
print(e)
'''
#點擊搜索
'''
#報錯代碼:selenium.common.exceptions.ElementNotVisibleExceptionsu=driver.find_element_by_id('su')
su.click()
file_name2=root_dir+'/zhangziyi2.png'
driver.save_screenshot(file_name2)
'''
driver.execute_script("$('#su').eq(0).attr('style','height:20px;opacity:1;display:block;position:static;transform:translate(0px, 0px) scale(1)')")
click_btn=driver.find_element_by_id('su')
# su.click()
ActionChains(driver).click(click_btn).click()
time.sleep(5)?#以免網絡慢(超時)沒能取到元素
file_name2=root_dir+'/zhangziyi2.png'
driver.save_screenshot(file_name2)
總結
以上是生活随笔為你收集整理的软件测试爬虫,【松勤软件自动化测试】Python3-爬虫~selenium\phantomjs\ActionChains百度例子...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: autojs教程:多多果林app脚本代码
- 下一篇: win10软件卸载残留删除的一种方法