Reputation: 100
I'm trying to switch to this frame
However, I kept getting this error:
Traceback (most recent call last):
File "/Users/yaoweiqi/PycharmProjects/learnselenium/test1.py", line 29, in <module>
browser.switch_to.frame('layui-layer-iframe6')
File "/Users/yaoweiqi/PycharmProjects/learnselenium/venv/lib/python3.6/site-packages/selenium/webdriver/remote/switch_to.py", line 89, in frame
self._driver.execute(Command.SWITCH_TO_FRAME, {'id': frame_reference})
File "/Users/yaoweiqi/PycharmProjects/learnselenium/venv/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/Users/yaoweiqi/PycharmProjects/learnselenium/venv/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchFrameException: Message: no such frame
(Session info: chrome=70.0.3538.77)
(Driver info: chromedriver=2.43.600229 (3fae4d0cda5334b4f533bede5a4787f7b832d052),platform=Mac OS X 10.13.1 x86_64)
Can anyone tell me what am I doing wrong? Thanks a lot. Following is my code:
from selenium import webdriver
import time
from wait_element import wait_element
browser = webdriver.Chrome('/Users/yaoweiqi/Downloads/chromedriver')
browser.get('http://47.99.113.178/index.html')
wait_element(driver=browser, xpath='//input[@class="user-aqm-input"]', action='input', keys='***')
wait_element(driver=browser, xpath='//input[@class="user-user-input"]', action='input', keys='***')
wait_element(driver=browser, xpath='//input[@class="user-pwd-input"]', action='input', keys='***')
wait_element(driver=browser, xpath='//button[@class="user-submit determine"]', action='click')
try:
wait_element(driver=browser, xpath='//img[@class="updataclose"]', action='click')
except:
print('notice already closed')
wait_element(driver=browser, xpath='//*[@class="icon-jiahao"]', action='click')
wait_element(driver=browser, xpath='//*[@class="housing-type"]', action='click')
time.sleep(5)
frames = browser.find_element_by_tag_name('iframe')
browser.switch_to.frame('layui-layer-iframe6')
Upvotes: 3
Views: 11920
Reputation: 1
Just a reminder, the name of the iframe might change with the number of clicks.
Here is an example:
for num in range(1, 2):
frame_id = "layui-layer-iframe" + str(num)
browser.switch_to.frame(frame_id)
Upvotes: 0
Reputation: 193088
As per the HTML to switch to the desired frame you need to use WebDriverwait for the frame to be available and switch to it and you can use either of the following solutions:
CSS_SELECTOR
:
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[id^='layui-layer-iframe'][src^='fangyuan']")))
XPATH
:
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[starts-with(@id,'layui-layer-iframe') and starts-with(@src,'fangyuan')]")))
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
Here you can find a relevant discussion on Ways to deal with #document under iframe
Upvotes: 2
Reputation: 2627
you are doing wrong, ayui-layer-iframe6
it must be layui-layer-iframe6
try this,
browser.switch_to.frame(browser.find_element_by_name("layui-layer-iframe6"))
and again come back to main window use,
browser.switch_to.default_content()
as traceback gives NoSuchFrameException
so putting wait would also work WebDriverWait()
.
Upvotes: 1
Reputation: 100
Problem solved. I used this code:
browser.switch_to.frame(browser.find_element_by_xpath('//*[@id="layui-layer-iframe6"]'))
Upvotes: -1