1.alert彈框
alert = driver.switch_to.alert()
TypeError: 'Alert' object is not callable#對象不可調用
對象不能用函數形式調用,就是不能callable。 此類錯誤一般是由於內部函數被用作變量名時發生。
正確用法:alert作為@property使用。
alert = driver.switch_to.alert
2.進行file文件上傳引入win32gui報錯
1)下載好pywin32
2)設置PYTHONPATH為:D:\Python34\Lib\site-packages
3)重啟pycharm,重新導入即可。
3.在做selenium web頁面的跳轉,當頁面發生變化時,一定要注意等待時間的設置。
如果在執行過程中,出現元素不存在或者未找到,或者執行結束未報錯,首先考慮是否進行等待時間的設置。
若等待時間添加了,看是否該元素在iframe中,如果在,直接定位是定位不到的,必須切換到iframe,再定位。
4.WebDriverException: Message: unknown error: Chrome failed to start: crashed
解決辦法:谷歌版本過低,下載一個最新版本,同時更新driver驅動版本為相對應的版本即可。
5.添加cookie操作時,WebDriverException:會出現報錯為:Message: unable to set cookie。報錯信息如下
Traceback (most recent call last):
File "D:/python_workshop/python6/selenium_webdriver/add_cookies的使用(二).py", line 9, in <module> driver.add_cookie(cookie_dict=cookies) File "D:\Program\python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 872, in add_cookie self.execute(Command.ADD_COOKIE, {'cookie': cookie_dict}) File "D:\Program\python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 312, in execute self.error_handler.check_response(response) File "D:\Program\python34\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.WebDriverException: Message: unable to set cookie (Session info: chrome=66.0.3359.139) (Driver info: chromedriver=2.35.528161 (5b82f2d2aae0ca24b877009200ced9065a772e73),platform=Windows NT 6.1.7600 x86_64)
解決方法:參考文檔--https://www.cnblogs.com/CoolJayson/p/7421013.html
需要在driver.add_cookie()前加上driver1.get("url地址")。
必須首先加載網站,這樣Selenium 才能知道cookie 屬於哪個網站,即使加載網站的行為對我們沒任何用處。如下代碼加粗部分。
driver1 = webdriver.Chrome() driver1.maximize_window() cookie2 = {"name":"BDUSS","value":"UJJTH5rcE9qd1VsWDZ5SzFsQkJVZUVPZTMwRi05bzg4UDE5YXJYZDAzNklkRzViQVFBQUFBJCQAAAAAAAAAAAEAAADzdT9HTGlseTAyNTE4AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIjnRluI50Zbe"} cookie3 = {"name":"BAIDUID","value":"F513A1C2ADA0A19C868F711C1792A3D0"} driver1.get("https://passport.baidu.com/center") #將獲取的兩個cookie都添加上 driver1.add_cookie(cookie2) driver1.add_cookie(cookie3)
6.selenium加入單元測試pytest后,提示:Empty test suite.
D:\Python34\python.exe "D:\Program Files (x86)\JetBrains\PyCharm Community Edition 3.4.4\helpers\pycharm\utrunner.py" E:\aaa\python\web_API\QCD_webFramework\pytest_TestCases\test_login_fixture.py true Testing started at 16:03 ... Process finished with exit code 0 Empty test suite
解決辦法:網上一部分百度說是,在setting設置,如下。我設置后運行還是之前的錯誤

然后又再次點擊“Edit Configurations”

進入如下界面,添加一個py.test


填入name,寫上路徑,點擊apply->點擊ok

在右上角選中選中剛剛配置的name名字,然后點擊run,即可。

