程序运行中遇到的问题总结:
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)