Handling drop down with selenium , X path method not working?

huangapple go评论108阅读模式
英文:

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

这是我的代码:

  1. self.driver.get('https://pynishant.github.io/dropdown-selenium-python-select.html')
  2. sleep(1)
  3. self.driver.maximize_window()
  4. sleep(2)
  5. wait = WebDriverWait(self.driver, 20)
  6. select = wait.until(EC.presence_of_element_located((By.CLASS_NAME, 'custom-select')))
  7. print(select)
  8. drop = self.driver.find_element(By.CLASS_NAME, 'custom-select')
  9. drop.click()
  10. sleep(2)
  11. sel = self.driver.find_element(By.XPATH, '//select[@id ="lang1"]/option[@value="3"]')
  12. sel.click()
  13. 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:

  1. self.driver.get(
  2. 'https://pynishant.github.io/dropdown-selenium-python-select.html')
  3. sleep(1)
  4. self.driver.maximize_window()
  5. sleep(2)
  6. wait = WebDriverWait(self.driver, 20)
  7. select = wait.until(EC.presence_of_element_located((By.CLASS_NAME, "custom-select")))
  8. print(select)
  9. drop = self.driver.find_element(By.CLASS_NAME,"custom-select")
  10. drop.click()
  11. sleep(2)
  12. sel = self.driver.find_element(By.XPATH,'//select[@id ="lang1"]/option[@value="3"]')
  13. sel.click()
  14. sleep(2)

答案1

得分: 1

问题:

您试图点击的内容未显示/存在于DOM内,而是该列表包含所有元素,但它们未在DOM上处理。

因此,您会收到错误消息:ElementNotInteractableException

解决方案:
您应该尝试点击DOM内显示的内容。

您可以尝试以下代码,应该可以正常工作。

  1. 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.

  1. sel = self.driver.find_element(By.XPATH, '//*[@class="select-items"]/div[3]')

huangapple
  • 本文由 发表于 2023年1月6日 13:31:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75027268.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定