lag37
lag37

Reputation: 51

Issue with Iframe Selenium [Python]

I am having a really tough time using Selenium with an iframe.

So basically the website is built like this :

  <tbody>
      <tr>
        <td>
          <iframe>
            <html>
          </iframe>
        </td>
      </tr>
  </tbody>
</table>
<table>

I want to access the html tag and use the send key there so the form that is displayed is filled automaticaly. I tried using the Xpath but it's not working. After around 3 days of trial and error and looking everywhere I can't find a solution I get errrors like this: frame.send_keys(Keys.TAB) AttributeError: 'list' object has no attribute 'send_keys'

I even tried sending the TAB key to select what I wnat but nothing is working.

frame = driver.find_elements_by_xpath(
        '/html/body/div[11]/div/div/form/div[1]/div/div/form/div[1]/div[2]/table/tbody/tr[2]/td')
    time.sleep(1)
    time.sleep(1)
    frame.send_keys(Keys.TAB)
    frame.send_keys("hello")

If someone has a solution I would be very grateful.

Best

Upvotes: 0

Views: 273

Answers (1)

cruisepandey
cruisepandey

Reputation: 29362

I would suggest you to switch to frame like this (with explicit waits):

wait = WebDriverWait(driver, 10)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "iframe xpath")))

Imports :

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

once you have switched you can send keys like this :

wait.until(EC.element_to_be_clickable((By.XPATH, "form xpath "))).send_keys("some string")

and once you are done, you should switch to default content like this :

driver.switch_to.default_content()

That is basically to switch to iframe, now coming to your code :

You have used find_elements_by_xpath which will return a list, and in your case you have given frame as a name for list.

In python list, we can not do send_keys, since send_keys are supported by Selenium.

Upvotes: 1

Related Questions