CSS和XPath类似,也需要一些语法,才能写出正确的,完整的css选择表达式。相关w3c介绍,可以看这里:https://www.w3school.com.cn/cssref/css_selectors.asp
练习场景:百度首页百度一下按钮
具体脚本:
# coding=utf-8 from selenium import webdriver driver = webdriver.Chrome() driver.maximize_window() driver.implicitly_wait(6) driver.get("https://www.baidu.com") try: driver.find_element_by_css_selector("#su") print('test pass : element found by css selector') except Exception as e: print("Exception found",format(e)) driver.quit()
参考文章:https://blog.csdn.net/u011541946/article/details/68927139