Selenium find_element_by_css_selector定位输入框报selenium.common.exceptions.NoSuchElementException的解决方法
早晨操作find_element_by_css_selector定位输入框的代码如下:
#打开搜狗搜索页面 driver.get('https://www.sogou.com/') driver.find_element_by_css_selector('#sec-input').send_keys(Keys.CONTROL,'v') sleep(2) driver.quit()
执行时报:
C:\Users\EDZ\Desktop\selenium_demo\venv\Scripts\python.exe C:/Users/EDZ/Desktop/selenium_demo/webdriver/Keys_Option.py Traceback (most recent call last): File "C:/Users/EDZ/Desktop/selenium_demo/webdriver/Keys_Option.py", line 33, in <module> driver.find_element_by_css_selector('#sec-input').send_keys(Keys.CONTROL,'v') File "C:\Users\EDZ\Desktop\selenium_demo\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 627, in find_element_by_css_selector return self.find_element(by=By.CSS_SELECTOR, value=css_selector) File "C:\Users\EDZ\Desktop\selenium_demo\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 1014, in find_element 'value': value})['value'] File "C:\Users\EDZ\Desktop\selenium_demo\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 318, in execute self.error_handler.check_response(response) File "C:\Users\EDZ\Desktop\selenium_demo\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 240, 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":"#sec-input"} (Session info: chrome=83.0.4103.116) Stacktrace: Backtrace: Ordinal0 [0x003E9563+2725219] Ordinal0 [0x002E8551+1672529]
搜狗输入的源码如下:
经检查是find_element_by_css_selector后面定位输入框class的格式有误
正确代码如下:
#打开搜狗搜索页面 driver.get('https://www.sogou.com/') driver.find_element_by_css_selector('.sec-input').send_keys(Keys.CONTROL,'v') sleep(2)
执行操作正常没有报错
C:\Users\EDZ\Desktop\selenium_demo\venv\Scripts\python.exe C:/Users/EDZ/Desktop/selenium_demo/webdriver/Keys_Option.py
Process finished with exit code 0
具体find_element_by_css_selector几种操作方法可以参考前几天的博客
地址如下:
https://www.cnblogs.com/gengwulovestudy/p/13373151.html
以上有问题随时讨论!