发表于: 2019-09-26 23:17:09
0 588
今天完成的事情:
自动化测试遇到的问题及解决方法
明天计划的事情:
遇到的问题:暂无
收获:
a、没有导入驱动,而是导入了文件夹
例子:
# 加载浏览器驱动
driver=webdriver.chrome()
报错:
/Users/guyongliang/PycharmProjects/untitled/venv/bin/python /Users/guyongliang/PycharmProjects/untitled/test1.py
Traceback (most recent call last):
File "/Users/guyongliang/PycharmProjects/untitled/test1.py", line 5, in <module>
driver=webdriver.chrome()
TypeError: 'module' object is not callable
Process finished with exit code 1
解决方法:修改driver,把文件夹改为驱动模块
# 加载浏览器驱动
driver=webdriver.Chrome()
b、打开技能树首页,直接点击登录
例子:
# 打开技能树首页
# 等待2秒
# sleep(2)
# 点击官网登录按钮,跳转登录界面
driver.find_element_by_link_text('登录').click()
sleep(2)
报错:
/Users/guyongliang/PycharmProjects/untitled/venv/bin/python /Users/guyongliang/PycharmProjects/untitled/test1.py
Traceback (most recent call last):
File "/Users/guyongliang/PycharmProjects/untitled/test1.py", line 14, in <module>
driver.find_element_by_link_text('登录').click()
File "/Users/guyongliang/PycharmProjects/untitled/venv/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 428, in find_element_by_link_text
return self.find_element(by=By.LINK_TEXT, value=link_text)
File "/Users/guyongliang/PycharmProjects/untitled/venv/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 978, in find_element
'value': value})['value']
File "/Users/guyongliang/PycharmProjects/untitled/venv/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/Users/guyongliang/PycharmProjects/untitled/venv/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"link text","selector":"登录"}
(Session info: chrome=77.0.3865.90)
Process finished with exit code 1
解决方法:添加sleep(),让页面加载一会儿
# 打开技能树首页
# 等待2秒
sleep(2)
# 点击官网登录按钮,跳转登录界面
driver.find_element_by_link_text('登录').click()
sleep(2)
c、元素定位错误
例子:
# 点击账号输入框,输入账号
driver.find_element_by_name('te').send_keys('15021189756')
sleep(2)
报错:
/Users/guyongliang/PycharmProjects/untitled/venv/bin/python /Users/guyongliang/PycharmProjects/untitled/test1.py
Traceback (most recent call last):
File "/Users/guyongliang/PycharmProjects/untitled/test1.py", line 18, in <module>
driver.find_element_by_name('te').send_keys('15021189756')
File "/Users/guyongliang/PycharmProjects/untitled/venv/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 496, in find_element_by_name
return self.find_element(by=By.NAME, value=name)
File "/Users/guyongliang/PycharmProjects/untitled/venv/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 978, in find_element
'value': value})['value']
File "/Users/guyongliang/PycharmProjects/untitled/venv/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/Users/guyongliang/PycharmProjects/untitled/venv/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[name="te"]"}
(Session info: chrome=77.0.3865.90)
Process finished with exit code 1
解决方法:要能用F12,查找页面的元素位置,使用元素定位准确定位,(多练练元素定位)
# 点击账号输入框,输入账号
driver.find_element_by_name('tel').send_keys('15021189756')
sleep(2)
评论