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

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

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]')

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:

确定