1. 程式人生 > >python開發中遇到的問題

python開發中遇到的問題

 

程式執行中遇到的問題總結:

1,程式執行報錯

SyntaxError: Non-UTF-8 code starting with '\xe5' in file /Users/tiger007/Desktop/shell_test/Data/flask_up_down/flask_up/save_gmv.py on line 159, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details,

錯誤原因:程式中插入中文後的編碼問題(一般是在python2中出現這個問題)

解決方法:在程式開始加入下面一句話

# -*- coding: utf-8 -*-

2,Windows上執行程式報錯:

from pyqt5 import qtcore, qtwidgets importerror: DLL load failed: 找不到指定的程式

解決:不要使用pip安裝pyqt5,使用exe檔案安裝,安裝時注意選擇python檔案路徑,pyqt5下載連結:https://sourceforge.net/projects/pyqt/ 

3,

XPCOMGlueLoad error for file /root/firefox/libxul.so: libXt.so.6: cannot open shared object file: No such file or directory Couldn’t load XPCOM.

 解決:

yum provides libXt.so.6yum -y install libXt.x86_64

4, selenium xpath定位元素時出現如下錯誤資訊(注意:之前切換到了iframe):

selenium.common.exceptions.WebDriverException: Message: TypeError: can't access dead object

原因:原因是程式碼中用到了frame,獲取元素前需要切換到frame才能定位到元素,否則無法定位到元素

解決:在查詢之前加一句:driver.switch_to_default_content() 退出frame

5,selenium 準備點選退出時,報錯:

selenium.common.exceptions.ElementClickInterceptedException: Message: Element <a class="ebase-frame-header-userLink" href="https://login.taobao.com/member/logout.jhtml?f=top&redirectURL=https://sycm.taobao.com/portal/home.htm"> is not clickable at point (1274,41.66667175292969) because another element <div class="ebase-ImageTips__dsImageTips "> obscures it

錯誤原因:出現錯誤是因為螢幕上出現另一個具有相同類或相同xpath / css的元素。 嘗試給出一些等待方法,直到元素出現如Thread.sleep(),wait()。 

解決:兩種方法

# 1element = driver.find_element_by_css('div[class*="ebase-ImageTips__dsImageTips "]')driver.execute_script("arguments[0].click();", element)# 2element = driver.find_element_by_css('div[class*=" ebase-ImageTips__dsImageTips "]')webdriver.ActionChains(driver).move_to_element(element ).click(element ).perform()

 6,檢視Linux系統位數

命令:arch

i686是32位,x86_64位64位

7,在centos7,無介面上執行headless Chrome 遇到報錯 

Message: unknown error: Chrome failed to start: exited abnormally   (unknown error: DevToolsActivePort file doesn't exist)   (The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)

解決方法:新增引數chrome_options.add_argument('--no-sandbox')

chrome_options = Options()chrome_options.add_argument('--headless')chrome_options.add_argument('--no-sandbox')chrome_options.add_argument('user-agent=%s' % random.choice(MY_USER_AGENT_PC))driver = webdriver.Chrome(executable_path='/usr/local/bin/chromedriver', chrome_options=chrome_options)driver.set_window_size(1366, 1000)  # 調整頁面尺寸driver.implicitly_wait(3)