繁体   English   中英

Python Selenium 错误:元素不可交互

[英]Python Selenium ERROR: element not interactable

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

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)```

找到元素时会发生 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM