ApathyBear
ApathyBear

Reputation: 9595

Can't locate this input element selenium

I am trying to locate the input “to” field on this webpage: Link here (sorry about having to login to see it)

My code:

name_field = driver.find_element_by_xpath("//form[@id='compose-message']/div[6]/div/div/div[2]/div/div/textarea")

But that gives the following error:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//form[@id='compose-message']/div[6]/div/div/div[2]/div/div/textarea"}
  (Session info: chrome=93.0.4577.63)

For some reason this page is the only page that has given me any issue.

Upvotes: 0

Views: 590

Answers (4)

cruisepandey
cruisepandey

Reputation: 29362

Most of the textarea web elements are wrapped inside iframes.

Using explicit waits, does give your script a stability that it need.

Also prefer to use css selector over xpath.

In Selenium, to access elements which are inside of iframe, we need to switch the focus of webdriver to iframe. Like below

WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe[src^='/message/compose/']")))

and once you are inside this iframe you can interact with to input web element. Like below

WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.NAME, "to"))).send_keys('something here')
WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//span[text()='message']/following-sibling::div/descendant::textarea"))).send_keys('Some message')

Imports :

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

Also, remember to switch to default content when you are done with iframe.

driver.switch_to.default_content()

In order to work with elements which are outside of this iframe.

Upvotes: 1

Fahad Bhuyian
Fahad Bhuyian

Reputation: 325

use command CSS selector instead of XPath such as use below code.

yourwebsriver.find_elements_by_css_selector("textarea[name=text]").send_keys('something here')

Upvotes: 1

Prophet
Prophet

Reputation: 33351

First of all that element is inside an iframe. To access it you need to switch to that iframe. Also, your locator are not looking good. Please try the following code:

wait = WebDriverWait(driver, 20)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[contains(@src,'message')]")))
name_field = wait.until(EC.visibility_of_element_located((By.XPATH, "//input[@name='to']")))

To use the above expected conditions you will 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

Upvotes: 1

pmadhu
pmadhu

Reputation: 3433

The Element you are trying to find is in an iframe. Was able to find and send text to the elements with below code.

from selenium import webdriver
from selenium.webdriver import ChromeOptions
import time

opt = ChromeOptions()
opt.add_argument("--user-data-dir=C:\\Users\\*****\\AppData\\Local\\Google\\Chrome\\User Data")
driver = webdriver.Chrome(executable_path="path to chromedriver.exe",options=opt)

driver.maximize_window()
driver.implicitly_wait(10)

driver.get("https://www.reddit.com/message/compose/")
driver.switch_to.frame(driver.find_element_by_xpath("//iframe[@class='saPujbGMyXRwqISHcmJH9']"))
driver.find_element_by_xpath("//input[@name='to']").send_keys("Sample Text")
driver.find_element_by_xpath("//div[@class='usertext']//textarea").send_keys("Sample Text")

time.sleep(5)
driver.quit()

Upvotes: 1

Related Questions