使用iframe的Python Selenium webdriver

时间:2016-09-21 19:11:35

标签: python selenium iframe

美好的一天。我试图通过将我做的任何测试导出到Python 2.7来学习如何在firefox和Python上使用Selenium IDE。

在我的测试中我遇到了一些问题,其中一个问题是它没有识别2个文本字段,这些字段位于iframe内部。我已经在堆栈溢出时找到了其他一些答案,但我不确定如何在我的代码上应用它们。这是我从firefox上的Selenium IDE直接导出的结果。我对python和编程也是全新的,所以任何建议都会受到欢迎。

这就是我现在所拥有的:

# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re

class StiMPythonWebdriver(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)
        self.base_url = "https://webpage.com/"
        self.verificationErrors = []
        self.accept_next_alert = True

    def test_sti_m_python_webdriver(self):
        driver = self.driver
        driver.find_element_by_id("SEARCHS").clear()
        driver.find_element_by_id("SEARCHS").send_keys("403627") **It inserts a code of a form**
        driver.find_element_by_id("splitbutton1-button").click()
        # ERROR: Caught exception [ERROR: Unsupported command [waitForPopUp | details403627 | 100000]]
        # ERROR: Caught exception [ERROR: Unsupported command [selectWindow | name=details403627 | ]] **It waits a little for the pop up window to open so it can continue the test**
        driver.find_element_by_id("editButton_textSpan").click()
        Select(driver.find_element_by_id("status")).select_by_visible_text("Option 1")
        # ERROR: Caught exception [ERROR: Unsupported command [selectFrame | id=descrpt_ifr | ]]
        # ERROR: Caught exception [ERROR: Unsupported command [selectFrame | id=tinymce | ]] **Right here at this part it is supposed to select the <p> inside the Iframe and type the following sentence "Canceled"**


        driver.find_element_by_id("descrpt_ifr").clear()
        driver.find_element_by_id("descrpt_ifr").send_keys("Canceled")
        # ERROR: Caught exception [ERROR: Unsupported command [selectWindow | name=details403627 | ]]
        driver.find_element_by_id("goButton_textSpan").click()**then it selects a button that exits the pop up**

1 个答案:

答案 0 :(得分:1)

如果您想切换到iframe

driver.switch_to_frame(driver.find_element_by_xpath("//iframe[@id='tinymce ']"))

如果要切换到窗口-1似乎是默认活动窗口

driver.switch_to_window(driver.window_handles[-1])

完成后不要忘记切换回来。