Andrew
Andrew

Reputation: 1575

How do I send text into a textarea through Selenium and Python as per the HTML

Cannot figure out how can I write text into a popup, here is how the pop-up looks like: enter image description here

<textarea style="position: absolute; padding: 0px; width: 1px; height: 1em; outline: currentcolor none medium;" autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" wrap="off"></textarea>

This is how I tried to access it by using XPath:

driver.find_element_by_xpath("/html/body/div/div[1]/textarea").send_keys("Some text here")

Getting error that element is not found on the page:

selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: /html/body/div/div[1]/textarea

I used as well css_selector to access element, but still same error. How can I access popup properly?

Here is more HTML code: https://pastebin.com/6jdix2Cm

Upvotes: 0

Views: 2123

Answers (2)

cruisepandey
cruisepandey

Reputation: 29362

As per your response , that this textarea is in iframe.

First you will have to switch to frame, then you can interact with this textarea.

For switching to iframe, you can use this code :

driver.switch_to.frame(driver.find_element_by_xpath("//iframe[@src='https://qsm.qoo10.sg/gmkt.inc.gsm.web/common/scripts/module/tiny_mce_4.5.7/source/plugins/codemirror/source.html']"))  

then you can interact with textarea as :

driver.find_element_by_css_selector("textarea[spellcheck='false'][wrap='off'][style$='outline: currentcolor none medium;']").send_keys("Some text here")  

It is always good to switch to default content, once you are done with the particular iframe.For that you will need this code :

driver.switch_to.default_content()

Hope this will help.

Upvotes: 4

undetected Selenium
undetected Selenium

Reputation: 193088

As per the HTML you have shared, as the <textarea> is within an <iframe> so you need to induce WebDriverWait to switch to the desired frame and then again induce WebDriverWait for the desired element to be clickable before sending the character sequence and you can use the following solution:

WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[contains(@src,'gmkt.inc.gsm.web/common/scripts/module/tiny_mce_4.5.7/source/plugins/codemirror/source.html')]")))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.TAG_NAME, "textarea"))).send_keys("Andrew")

Note : You have to add the following imports :

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

Upvotes: 0

Related Questions