I am writing a script to automate some unnecessary teaching feedback.
I am using selenium/python to scrape data (student names & classes etc) and input the feedback. Everything works fine apart from inputting the feedback itself into the textarea element. Selenium gives me an ElementNotInteractable exception when using click() or send_keys().
I then tried using JS commands, eg:
comment = 'Thanks for coming to the lesson. Well done today!'
self.driver.execute_script(f'document.querySelector("#teacherCommentContent").innerText = "{comment}";')
I also tried editing in the chrome console using:
document.getElementById(teacherCommentContent).innerHTML = "message"
document.getElementById(teacherCommentContent).textContent = "message"
document.getElementById(teacherCommentContent).value = "message"
$(#teacherCommentContent).val("message")
None have been successful at updating the text in the textarea on screen, and as such I can't submit the feedback. Of course, I can manually enter the feedback, but that defeats the point.
See picture of website and HTML
CodePudding user response:
It looks like that website is behind a login, so I cannot test it. However, there are a few problems with your code:
- You are not waiting for the elements to fully load
- Did you check, is that textarea in an iframe, or shadowroot element?
- You can send keys (text) with Selenium
To wait for the element to load properly before trying to interact with it, you can use the following locator:
textarea_element = WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.ID, 'teacherCommentContent')))
To input some text in it, you can do (after you waited for the element to load, like above):
textarea_element.send_keys('An amazing teacher!')
You also need the following imports:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
Lastly, Selenium documentation can be found at https://www.selenium.dev/documentation/
Again, I cannot test this. You mentioned you are using selenium/python, so this would be a possible solution.
CodePudding user response:
For textarea you want to set innerText (not value). But for PWAs that often won't be enough, you also must dispatch a change event to the textarea. You will find a way to do that if you google it.