繁体   English   中英

Python selenium:元素不可交互

[英]Python selenium: Element not interactable

我尝试使用 python 编写 web 自动化,但总是收到错误警告:

selenium.common.exceptions.ElementNotInteractableException:消息:元素不可交互

这是代码:

def commentAndLike():
random.seed()
likeOrNext = random.randint(0, 3)

if likeOrNext == 0:
    if check_exists_by_xpath("/html/body/div[3]/div[2]/div/article/div[2]/section[1]/span[1]/button/span"):
        likeButton = browser.find_element_by_xpath("/html/body/div[3]/div[2]/div/article/div[2]/section[1]/span[1]/button/span")
        likeButton.click()

        time.sleep(randomNumber(6, 10))

        if check_exists_by_xpath("/html/body/div[3]/div[2]/div/article/div[2]/section[1]/span[2]/button/span"):
            random.seed()
            randomComment = random.randint(0, 3)

            textArea = browser.find_element_by_xpath("/html/body/div[3]/div[2]/div/article/div[2]/section[3]/div[1]/form/textarea")

            if randomComment == 0:
                textArea.send_keys(comments[0])
            elif randomComment == 1:
                textArea.send_keys(comments[1])
            elif randomComment == 2:
                textArea.send_keys(comments[2])
            elif randomComment == 3:
                textArea.send_keys(comments[3])

            time.sleep(randomNumber(15,30))

            postButton = browser.find_element_by_xpath("/html/body/div[3]/div[2]/div/article/div[2]/section[3]/div/form/button")
            postButton.click()

            time.sleep(randomNumber(20,25))

            nextButton2 = browser.find_element_by_xpath("/html/body/div[3]/div[1]/div/div/a[2]")
            nextButton2.click()

            time.sleep(randomNumber(15,20))

        else:
            nextButton4 = browser.find_element_by_xpath("/html/body/div[3]/div[1]/div/div/a[2]")
            nextButton4.click()

            time.sleep(randomNumber(20, 25))

    else:
        nextButton1 = browser.find_element_by_xpath("/html/body/div[3]/div[1]/div/div/a[2]")
        nextButton1.click()

        time.sleep(randomNumber(20, 25))

else:
    nextButton = browser.find_element_by_xpath("/html/body/div[3]/div[1]/div/div/a[2]")
    nextButton.click()

    time.sleep(randomNumber(20,25))

尝试将send_keys发送到textArea元素时会引发错误。

我知道我仍然需要改进代码并将 nextButton 缩短,但我想首先解决问题。 谢谢你的帮助!!

根据向textArea发送键引发错误的问题描述进行了修改。

您可以尝试使用 Javascript 来设置 textarea 的值:

browser.execute_script("arguments[0].value = 'myValue';", textArea)

如果这不起作用,您可以尝试先单击 textArea 来激活它:

browser.execute_script("arguments[0].click();", textArea)

browser.execute_script("arguments[0].value = 'myValue';", textArea);

在您的情况下,您可以将'myValue'替换为' + comments[0] + '

抛出这种异常表示虽然一个元素存在于 DOM 上,但它不在可以交互的 state 中。 您可以尝试实现某种等待机制,最好是这样的显式等待机制:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

textArea = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.XPATH, "/html/body/div[3]/div[2]/div/article/div[2]/section[3]/div[1]/form/textarea"))

暂无
暂无

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

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