本文章用于记录使用selenium时了解的报错。
本人学习使用的网课:Python+Selenium4框架介绍_哔哩哔哩_bilibili
【Web自动化测试】引入Pytest_哔哩哔哩_bilibili
一、基础学习
遇到报错推荐到故障排除协助 | Selenium中了解为什么。
1.pip install selenium
安装失败,报错ERROR: Exception: Traceback (most recent call last): .......
原因:
1.网络不好,换一个网络
2.到python目录中的Script中,cmd执行pip install -U selenium,多执行几次
3.确保Python的安装目录里没有其他软件的安装包在。Python只能安装在一个单独的文件夹里。
4.可以用pycharm多种方法安装
2.selenium入门案例
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("http://www.baidu.com/")
报错:
Traceback (most recent call last): File "C:\Users\14403\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\common\driver_finder.py", line 38, in get_path path = SeleniumManager().driver_location(options) if path is None else path
......
selenium.common.exceptions.NoSuchDriverException: Message: Unable to obtain driver for chrome using Selenium Manager.
原因:
1.chromedriver.exe没放在python目录中(python配置环境变量的目录)
也可以添加环境变量,或者代码配置path:
from selenium.webdriver import ChromeService
CHROMEDRIVER_PATH = r"C:\Users\...\chromedriver.exe"
service = ChromeService(executable_path=CHROMEDRIVER_PATH)
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(service=service, options=options)
2.chromedriver.exe版本与chrome版本不匹配
chromedriver.exe下载:chromedriver下载_chromedriver113下载-CSDN博客
geckodriver.exe下载:CNPM Binaries Mirror
3.元素定位(根据ID)
element = driver.find_element(By.ID, "kw1").send_keys("学习")
报错:
Traceback (most recent call last):
File "C:\Users\14403\Desktop\pypycode\selecode\Elemens2.py", line 9, in <module>
element = driver.find_element(By.ID, "kw1").send_keys("selenium")
......
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="kw1"]"}
(Session info: chrome=118.0.5993.71); For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#no-such-element-exception
原因:
1.没有id="kw1",id输入错误。
4.元素定位(根据class_name)
driver.find_elements(By.CLASS_NAME, 'channel-link').click()
报错:
Traceback (most recent call last):
File "C:\Users\14403\Desktop\pypycode\selecode\Elements3.py", line 13, in <module>
driver.find_elements(By.CLASS_NAME, 'channel-link').click()
AttributeError: 'list' object has no attribute 'click'
原因:
find_elements是返回列表,列表里面的对象可以进行点击操作,但列表不能点击,所以find_elements().click()是错误的。find_elements()[i].click()是可行的。
而find_element返回的是第一个符合定位的元素。
driver.find_elements_(xxx)[0] = driver.find_element_(xxxx)
driver.find_element(By.CLASS_NAME, 'icon-bg icon-bg__channel').click()
报错:
Traceback (most recent call last):
File "C:\Users\14403\Desktop\pypycode\selecode\Elements3.py", line 15, in <module>
driver.find_element(By.CLASS_NAME, 'icon-bg icon-bg__channel').click()
......
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".icon-bg icon-bg__channel"}
(Session info: chrome=118.0.5993.71); For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#no-such-element-exception
原因:
class name中不能有空格,'icon-bg icon-bg__channel' 只能保留其中一个,如 'icon-bg'
二、实战常见错误
1. 页面还没加载完成,导致找不到element
这种情况使用time.sleep()的方式。
2. element被下拉选择框挡住,导致找不到element
使用ActionChains操作鼠标移到别处点击一下,除去下拉框
from selenium.webdriver import ActionChains, Keys
driver = webdriver.Chrome()
open_url("https://train.qunar.com/")
driver.get(url)
driver.maximize_window()
action = ActionChains(driver)
action.move_to_element(xpath('//*[@id="js-tab-tit"]/li/a')).click().perform()