【问题标题】:Python-Selenium "element not interactable"Python-Selenium“元素不可交互”
【发布时间】:2021-12-16 16:01:45
【问题描述】:

我试图用硒填充数独,但每次都提出一个 不可交互的元素

相关代码:

from selenium import webdriver

driver = webdriver.Chrome()
driver.get('https://sudokutable.com/')

driver.find_element_by_xpath('//*[@id="body_wrapper"]/main/section[2]/div[1]/div[2]/div[1]').send_keys('1')

我也尝试过,但结果相同:

sleep(1)
num = driver.find_element_by_xpath('//*[@id="body_wrapper"]/main/section[2]/div[1]/div[2]/div[1]')
num.click()
num.send_keys('1')

如果有人知道任何发送密钥的方法,我非常感谢

【问题讨论】:

    标签: python selenium automation sudoku


    【解决方案1】:

    首先我们需要关闭 Cookie 弹窗。

    然后我们需要找到空单元格并尝试填充单元格。空单元格可以通过svg 标签中的class 属性识别。

    DOM when the cell is already filled
    <div class="game-grid__cell" data-group="1" data-row="0" data-column="4">
        <svg class="default" viewBox="0 0 72 72">
    
    DOM when the cell is empty
    <div class="game-grid__cell" data-group="0" data-row="0" data-column="1">
        <svg class="" viewBox="0 0 72 72">
    
    # Imports required:
    from selenium.webdriver import ActionChains
    from selenium.webdriver.support.wait import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.by import By
    
    driver.get("https://sudokutable.com/")
    actions =ActionChains(driver)
    wait = WebDriverWait(driver,30)
    
    # Close Cookie pop-up
    wait.until(EC.element_to_be_clickable((By.ID,"agree_cookies"))).click()
    
    # Find empty cells and use indexing to fill them up
    cell = driver.find_elements_by_xpath("//div[@class='game-grid__group'][1]/div/*[name()='svg' and @class='']")
    actions.move_to_element(cell[0]).click().send_keys("1").perform()
    

    【讨论】:

      【解决方案2】:

      试试这个而不是num.click():

      driver.execute_script("arguments[0].click();", num)
      

      要发送密钥,请使用:

      driver.find_element_by_tag_name('body').send_keys('1')
      

      【讨论】:

        猜你喜欢
        • 2020-03-08
        • 1970-01-01
        • 2021-08-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-22
        • 1970-01-01
        相关资源
        最近更新 更多