How to switch to nested framesets using Python Selenium?

681 Views Asked by At

I need to reach a target frame with Xpath = html/frameset/frameset[1]/frame[2].

The HTML tree of the web site is as follows (the target frame is marked with a red *): enter image description here

The web page is a chat room and I want to get the input box and the send button.

The following code logs into the chat room and tries to get to the target frame, but it fails there (Selenium hangs while trying to switch to that frame):

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

driver = webdriver.Chrome('/home/yky/Downloads/chromedriver')
driver.get('http://ip131.ek21.com/oaca_1/?ot=1')

### Log into chat room:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="mlogin"]/form/ul/li[1]/input'))).send_keys("UserOne")
driver.find_element_by_xpath('//*[@id="mlogin"]/form/div/span').click()
time.sleep(6)

### NONE of these works:
#driver.switch_to.frame(driver.find_element_by_css_selector("frame[name='ta']"))
#driver.switch_to.frame("ta")
#driver.switch_to_frame(driver.find_element_by_xpath('html/frameset/frameset[1]/frame[2]'))
#driver.switch_to.frame(2);

inputbox = driver.find_element_by_name("says_temp")
sendbutton = driver.findElement(By.xpath("//input[@value='送出']"));

I find the suggestion to ignore framesets to be very beguiling, as it doesn't make sense when the framesets contain other frames as a tree structure. Also, the problem is that the first frameset does not contain any frame; it just contains another frameset. So there is no way for Selenium to switch to the first frameset.

Please help!!

0

There are 0 best solutions below