英文:
Handling drop down with selenium , X path method not working?
问题
我正在尝试点击测试语言下拉列表中的Python,其id为lang1
,但我在这里遇到错误,错误信息为selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable: Element is not currently visible and may not be manipulated
,我尝试的XPath是//select[@id ="lang1"]/option[@value="3"]
。
网站链接:https://pynishant.github.io/dropdown-selenium-python-select.html
这是我的代码:
self.driver.get('https://pynishant.github.io/dropdown-selenium-python-select.html')
sleep(1)
self.driver.maximize_window()
sleep(2)
wait = WebDriverWait(self.driver, 20)
select = wait.until(EC.presence_of_element_located((By.CLASS_NAME, 'custom-select')))
print(select)
drop = self.driver.find_element(By.CLASS_NAME, 'custom-select')
drop.click()
sleep(2)
sel = self.driver.find_element(By.XPATH, '//select[@id ="lang1"]/option[@value="3"]')
sel.click()
sleep(2)
英文:
I am trying to click Python present in the testlang dropdown list with id of select as lang1
but I am getting error here as selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable: Element is not currently visible and may not be manipulated
, xpath I am trying is //select[@id ="lang1"]/option[@value="3"]
website https://pynishant.github.io/dropdown-selenium-python-select.html
This is my code:
self.driver.get(
'https://pynishant.github.io/dropdown-selenium-python-select.html')
sleep(1)
self.driver.maximize_window()
sleep(2)
wait = WebDriverWait(self.driver, 20)
select = wait.until(EC.presence_of_element_located((By.CLASS_NAME, "custom-select")))
print(select)
drop = self.driver.find_element(By.CLASS_NAME,"custom-select")
drop.click()
sleep(2)
sel = self.driver.find_element(By.XPATH,'//select[@id ="lang1"]/option[@value="3"]')
sel.click()
sleep(2)
答案1
得分: 1
问题:
您试图点击的内容未显示/存在于DOM内,而是该列表包含所有元素,但它们未在DOM上处理。
因此,您会收到错误消息:ElementNotInteractableException
解决方案:
您应该尝试点击DOM内显示的内容。
您可以尝试以下代码,应该可以正常工作。
sel = self.driver.find_element(By.XPATH, '//*[@class="select-items"]/div[3]')
英文:
The Problem:
What you are trying to click is not displayed/present inside the DOM, it's the list which has all the elements present, but they are not processed on the DOM.
Hence you get the error: ElementNotInteractableException
The Solution:
You should try to click what's present inside the DOM.
you can try the below code and it should work fine.
sel = self.driver.find_element(By.XPATH, '//*[@class="select-items"]/div[3]')
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论