html - 切换到以前的 IFRAME Selenium Python

标签 html python-2.7 selenium selenium-webdriver

我正在尝试切换到上一帧。

在我的代码中

  1. 我切换到框架(BRCT1Frame)
  2. 点击打开新窗口的按钮
  3. 切换到新窗口
  4. 切换到窗口内的框架
  5. 插入数据并保存,同时关闭窗口和框架

但是,我需要切换到 BRVT1Frame 之前的帧。

这是我的代码:

BRT1Frame=driver.switch_to_frame(driver.find_element_by_tag_name("iframe"))
    buttons=driver.find_elements_by_css_selector("button")
    for button in buttons:
        if button.text.strip()=="Create Community":
            button.click()

    #A new pop up window causes me to switch windows
for handle in driver.window_handles:
    BRT=driver.switch_to_window(handle)
    print(BRT)

#then I switch frames
driver.switch_to_frame(driver.find_element_by_class_name("FormDialogIFrame"))   
driver.find_element_by_id("CommunityFormCommunityNameTextBox").send_keys("1st MEB STAFFEX")

save=driver.find_element_by_id("CommunityFormSaveButton").click()
#that popup closes now I have to switch to another frame

我尝试切换到代码的框架:

<iframe role="presentation" 
class="x-component x-window-item x-component-default"
src="/brvt?lang=en_US&amp;owf=true&amp;themeName=a_default&amp;
themeContrast=standard&amp;themeFontSize=12" 
name="{&quot;id&quot;:&quot;778a1259-bc80-602e-c0ad-16a3f9220516&quot;,&quot;containerVersion&quot;:&quot;7-GA&quot;,&quot;webContextPath&quot;:&quot;/owf&quot;,&quot;preferenceLocation&quot;:&quot;https://www.url,com;,&quot;relayUrl&quot;:&quot;https://url.com/55js/even/rpc_relay.uncompressed.html&quot;,&quot;lang&quot;:&quot;en_US&quot;,&quot;currentTheme&quot;:{&quot;themeName&quot;:&quot;a_default&quot;,&quot;themeContrast&quot;:&quot;standard&quot;,&quot;themeFontSize&quot;:12},&quot;55f&quot;:true,&quot;layout&quot;:&quot;desktop&quot;,&quot;url&quot;:&quot;/brvt&quot;,&quot;guid&quot;:&quot;260f0022-66de-a78b-3ce8-8de63a3bdbec&quot;,&quot;version&quot;:1,&quot;locked&quot;:false}" 
id="{&quot;id&quot;:&quot;778a1259-bc80-602e-c0ad-16a3f9220516&quot;}" 
frameborder="0"></iframe>

我尝试过这样做:

driver.switch_to_frame(driver.find_element_by_tag_name("iframe"))

这有效,但是当我尝试单击框架内的下拉菜单时:

driver.find_element_by_css_selector("#OYk3vd6NKj1jbrnew > td.sub_item_text").click()

我也尝试过:

 driver.switch_to_frame(driver.find_element_by_id("{&quot;id&quot;:&quot;778a1259-bc80-602e-c0ad-16a3f9220516&quot;}")

我收到一条错误,指出无法找到该元素。 有建议吗?

最佳答案

如果您只想精确定位指定的帧,请尝试:

required_frame = driver.find_element_by_xpath('//iframe[@role="presentation"]')
driver.switch_to_frame(required_frame)

或者更具体:

required_frame = driver.find_element_by_xpath('//iframe[@role="presentation"][@class="x-component x-window-item x-component-default"]')
driver.switch_to_frame(required_frame)

这个建议有帮助吗?

关于html - 切换到以前的 IFRAME Selenium Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33998910/

相关文章:

java - 测试脚本中的 Thread.sleep 替代品

python - len(browser.find_elements_by_class_name ('foo' )) 给出 0

实际有效的Python html解析

macos - 当我运行 ipython Notebook --pylab inline 时浏览器中出现空白页

python - sys.maxsize 当前可能的值是多少?

python - 数据库错误 : ORA-01036: illegal variable name/number

java selenium xpath 获取字符串值

html - 为什么我的 table 旁边有一个随机空间?

html - 创建定时图像幻灯片放映,其中每个图像都是一个链接?

javascript - contenteditable 中无法访问的空列表项