日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

软件测试爬虫,【松勤软件自动化测试】Python3-爬虫~selenium\phantomjs\ActionChains百度例子...

發布時間:2024/3/24 python 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 软件测试爬虫,【松勤软件自动化测试】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百度例子...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。