简体   繁体   中英

Python selenium: Element not interactable

I tried to code a web automation with python but always get the error warning:

selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable

Here is the code:

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

The error gets thrown when attempting to send_keys to the textArea element.

I know I will still have to improve the code and shorten the nextButton out, but I want to solve the problems at first. Thank you for all your help!!

Modified based on problem description where sending keys to textArea is throwing an error.

You can try to use Javascript to set the value of the textarea:

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

If this does not work, you could try clicking the textArea to activate it first:

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

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

In your case, you would replace 'myValue' with ' + comments[0] + '

This kind of exception is thrown to indicate that although an element is present on the DOM, it is not in a state that can be interacted with. You can try to implement some kind of wait mechanism, preferably an explicit one like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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