Python Selenium ERROR: element not interactable(元素不可交互)

问题描述 投票:0回答:1

这是我破解密码游戏的代码:最终它说密码圈(容器)是不可交互的,我如何解决这个问题?)

from time import sleep


driver = webdriver.Chrome()
driver.get('https://meduza.io/games/bud-hakerom-igra-meduzy')
xpath = driver.find_element_by_xpath
start_button = xpath('//*[@id="maincontent"]/div/div/div[2]/div[2]/div[3]/button')
start_button.click()
sleep(4)
driver.switch_to.frame('/embed/cows')
play_button = xpath('//*[@id="app"]/div/form/div[1]/div[1]/button')
play_button.click()
sleep(1)
first_container = xpath('//*[@id="app"]/div/div/div[1]/div[1]/div/div/span[1]')
second_container = xpath('//*[@id="app"]/div/div/div[1]/div[1]/div/div/span[2]')
third_container = xpath('//*[@id="app"]/div/div/div[1]/div[1]/div/div/span[3]')
fourth_container = xpath('//*[@id="app"]/div/div/div[1]/div[1]/div/div/span[4]')
for x in range(10):
    for y in range(10):
        for z in range(10):
            for t in range(10):
                first_container.send_keys(x)
                second_container.send_keys(y)
                third_container.send_keys(z)
                fourth_container.send_keys(t)```
python selenium selenium-webdriver
1个回答
1
投票

ElementNotInteractableException是当发现一个元素,但你不能与之交互时发生的,你可以用actionChains来解决。

有这么多的原因。

元素不可见 不显示 元素关闭 尖叫 元素在另一个元素后面或被隐藏了

找到下面的代码,我已经测试了这个硬编码值。

driver.get('https://meduza.io/games/bud-hakerom-igra-meduzy')
xpath = driver.find_element_by_xpath
start_button = xpath('//*[@id="maincontent"]/div/div/div[2]/div[2]/div[3]/button')
start_button.click()
time.sleep(5)
driver.switch_to.frame('/embed/cows')
play_button = xpath('//*[@id="app"]/div/form/div[1]/div[1]/button')
play_button.click()
time.sleep(5)
first_container = xpath('//*[@id="app"]/div/div/div[1]/div[1]/div/div/span[1]')
second_container = xpath('//*[@id="app"]/div/div/div[1]/div[1]/div/div/span[2]')
third_container = xpath('//*[@id="app"]/div/div/div[1]/div[1]/div/div/span[3]')
fourth_container = xpath('//*[@id="app"]/div/div/div[1]/div[1]/div/div/span[4]')


for x in range(10):
    for y in range(10):
        for z in range(10):
            for t in range(10):
                actionChains = ActionChains(driver)
                actionChains.move_to_element(first_container).click().perform()
                actionChains.move_to_element(first_container).send_keys("2").perform()

                actionChains.move_to_element(second_container).click().perform()
                actionChains.move_to_element(second_container).send_keys("3").perform()

                actionChains.move_to_element(third_container).click().perform()
                actionChains.move_to_element(third_container).send_keys("3").perform()

                actionChains.move_to_element(fourth_container).click().perform()
                actionChains.move_to_element(fourth_container).send_keys("3").perform()

请注意。 请在您的解决方案中添加以下导入

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.chrome.options import Options
import time
from selenium.webdriver.common.action_chains import ActionChains
© www.soinside.com 2019 - 2024. All rights reserved.