0

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

Ratmir Asanov
  • 6,237
  • 5
  • 26
  • 40
Andrew
  • 1,507
  • 1
  • 22
  • 42
  • Seems like it is in iframe. can you share little bit more HTML around this `textarea` – cruisepandey Jul 26 '18 at 07:10
  • here is more code https://pastebin.com/6jdix2Cm. Textarea is wrapped in the iframe – Andrew Jul 26 '18 at 07:16
  • 1
    Possible duplicate of [Switching to iframe in selenium python](https://stackoverflow.com/questions/44834358/switching-to-iframe-in-selenium-python) – Guy Jul 26 '18 at 07:22
  • @Andrew : Can you give us update ? Was any one of the answer helpful ? or are you still facing any issue ? – cruisepandey Jul 26 '18 at 13:32
  • @cruisepandey Thanks a lot it solved my problem! – Andrew Jul 26 '18 at 15:02
  • Does this answer your question? [Select iframe using Python + Selenium](https://stackoverflow.com/questions/7534622/select-iframe-using-python-selenium) – Prophet Jun 04 '21 at 06:47

2 Answers2

4

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.

cruisepandey
  • 28,520
  • 6
  • 20
  • 38
  • 1
    driver.switch_to.frame() is deprecated and driver.switch_to.defaultContent() is not a python implementation of WebDriver API – Jan Rozycki Jul 26 '18 at 07:28
  • @JanRozycki : Which version of selenium are you talking about ? I have been working with selenium 3.12.0 and it works fine. – cruisepandey Jul 26 '18 at 07:30
  • 1
    Apart from that, since when python uses camel case and semicolons? ;) – Jan Rozycki Jul 26 '18 at 07:42
  • @JanRozycki , `switch_to.frame() ` is not deprecated. Deprecated one is `switch_to_frame()` – Andersson Jul 26 '18 at 07:45
  • 1
    @cruisepandey use `switch_to.default_content()` instead of `switch_to.defaultContent(); ` – Andersson Jul 26 '18 at 07:48
  • @JanRozycki : Can't you just read what they have mentioned : `switch_to_frame(frame_reference) Deprecated` use `driver.switch_to.frame` . Now that semi colons is a typo. Just get your facts clear before you make this kind of assumption and down voting. – cruisepandey Jul 26 '18 at 08:13
  • @Andersson : Yeah that was just a typo, I copied it. Although I have removed the semi colon. Thank you. – cruisepandey Jul 26 '18 at 08:15
  • Sorry for that @cruisepandey – Jan Rozycki Jul 26 '18 at 08:17
  • @JanRozycki : No problem. and you see that : somebody have found those comments as useful too. Don't make judgement too soon. Cheers ! – cruisepandey Jul 26 '18 at 08:18
0

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
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352