DevPy
DevPy

Reputation: 254

How to locate the element within an iframe using Selenium WebDriver through Python

While doing automation testing on this website http://www.scstrade.com/TechnicalAnalysis/tvchart/ i was unable to find the element using selenium.

i want to find the search bar element at the top which is used to search for stocks and then pass the name of desired stock in that bar using selenium.
Here is the xpath for the search bar :

/html/body/div[1]/div[2]/div/div/div[1]/div/div/div/div/div[1]/div/div/input

here is my code:

from selenium import webdriver
driver = webdriver.Chrome("D:\PyCharm Projects\Web Automation\drivers\chromedriver.exe")
driver.get("http://www.scstrade.com/TechnicalAnalysis/tvchart/")
driver.find_elements_by_xpath('/html/body/div[1]/div[2]/div/div/div[1]/div/div/div/div/div[1]/div/div/input')
Output : []

i also tried finding the element using the class name:

driver.find_element_by_class_name('input-3lfOzLDc-')

which gives me this error:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"class name","selector":"input-3lfOzLDc-"}
  (Session info: chrome=83.0.4103.61)
  (Driver info: chromedriver=2.38.552522 (437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb),platform=Windows NT 10.0.17134 x86_64)

The element i am trying to access only has the class name so i can't try using the id. I also tried switching to the frame first but i can't even find the frame element for this website using selenium.

Upvotes: 0

Views: 480

Answers (1)

undetected Selenium
undetected Selenium

Reputation: 193088

The search bar element at the top left corner is within an <iframe> so to invoke send_keys() on the element you have to:

  • Induce WebDriverWait for the desired frame_to_be_available_and_switch_to_it().
  • Induce WebDriverWait for the desired element_to_be_clickable().
  • You can use either of the following Locator Strategies:

    • Using CSS_SELECTOR:

      WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[src^='charting_library/static/en-tv-chart']")))
      index = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div#header-toolbar-symbol-search input")))
      index.click()
      index.clear()
      index.send_keys("KSE 30")
      
    • Using XPATH:

      WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[starts-with(@src, 'charting_library/static/en-tv-chart')]")))
      index = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[@id='header-toolbar-symbol-search']//input")))
      index.click()
      index.clear()
      index.send_keys("KSE 30")
      
  • 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
    

References

You can find a couple of relevant discussions in:

Upvotes: 1

Related Questions