我正在尝试 select 使用 selenium webdriver python 隐藏下拉菜单选项,但它抛出 "Message: no such element: Unable to locate element"

i am trying to select hidden dropdown menu option using selenium webdriver python but it throw "Message: no such element: Unable to locate element"

我正在尝试此代码

import requests as req
from selenium.webdriver.common.by import By
from selenium import webdriver
#from selenium.webdriver.support.ui import WebDriverWait
#from selenium.webdriver.support import expected_conditions as EC
#from selenium.webdriver.support.select import Select as select
driver = webdriver.Chrome(f'F:\Deep Learning\chromedriver')
driver.maximize_window()
driver.get('https://www.naukri.com/')
driver.implicitly_wait(10)
driver.find_element(By.XPATH,'//input[@placeholder="Enter skills / designations / companies"]').send_keys('data scientist')
driver.find_element(By.XPATH,'//input[@placeholder="Enter location"]').send_keys('pune,Delhi NCR,Hydrabad')
driver.find_element(By.CSS_SELECTOR,".dropdownMainContainer").click
driver.implicitly_wait(10)

#driver.find_element(By.XPATH,'//*[@id="expereinceDD"]').click
driver.implicitly_wait(10)
driver.find_element(By.XPATH,"//span[normalize-space()='2 years']").click
driver.quit()

但它抛出错误消息

之前点击“select experiance”它显示! select experiance screenshot before clicking on"select experiance" 单击“select 体验”下拉菜单后显示 after click on it

错误消息是“消息:没有这样的元素:无法定位元素” 作为参考,我给出了 link https://www.naukri.com/ error massege

我怎样才能点击“select 体验”菜单,以便 select 选择其中一个

您的主要问题是您尝试使用 non-existing .click 方法而不是 Selenium .click() 方法。
此外,无需多次定义 driver.implicitly_wait(10) 。此命令为整个会话设置隐式等待 driver
此代码有效:

import requests as req
from selenium.webdriver.common.by import By
from selenium import webdriver
#from selenium.webdriver.support.ui import WebDriverWait
#from selenium.webdriver.support import expected_conditions as EC
#from selenium.webdriver.support.select import Select as select
driver = webdriver.Chrome(f'F:\Deep Learning\chromedriver')
driver.maximize_window()
driver.get('https://www.naukri.com/')
driver.implicitly_wait(10)
driver.find_element(By.XPATH,'//input[@placeholder="Enter skills / designations / companies"]').send_keys('data scientist')
driver.find_element(By.XPATH,'//input[@placeholder="Enter location"]').send_keys('pune,Delhi NCR,Hydrabad')
driver.find_element(By.CSS_SELECTOR,".dropdownMainContainer").click()
driver.find_element(By.XPATH,"//span[normalize-space()='2 years']").click()
driver.quit()

如果您使用 click() 而不是 click,您的代码可以正常工作。

driver.find_element(By.CSS_SELECTOR,".dropdownMainContainer").click()


driver.find_element(By.XPATH,"//span[normalize-space()='2 years']").click()

此外,使用 explicit wait 而不是 implicit waitExplicit wait,我的意思是 WebdriverWait。它有助于良好的练习。