简体   繁体   English

Python Webdriver我的脚本无法在iFrame中找到按钮

[英]Python Webdriver my script won't find the button inside the iFrame

I am trying to verify if a button is present on a webpage after I have successfully logged in. I am using Webdriver with Python. 成功登录后,我试图验证网页上是否存在按钮。我将Webdriver与Python结合使用。 The button is in an iFrame. 该按钮位于iFrame中。 This is my first webdriver python program using the a page object model framework. 这是我的第一个使用页面对象模型框架的webdriver python程序。 Not bad so far i think. 到目前为止,我认为还不错。

The program successfully logs in but it won't find the iFrame. 该程序已成功登录,但找不到iFrame。 I need some help please. 我需要一些帮助。

I receive the following error: 我收到以下错误:

Error
Traceback (most recent call last):
File "C:\Users\riaz.ladhani\PycharmProjects\Selenium Webdriver\TestCore  01\LoginPage_TestCase.py", line 16, in test_login_valid_user
login_page.isAdministration_present()
File "C:\Users\riaz.ladhani\PycharmProjects\Selenium Webdriver\TestCore  01\page.py", line 44, in isAdministration_present
content_frame = self.driver.find_element(*MainPageLocators.contentFrame)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py",  line 664, in find_element
{'using': by, 'value': value})['value']
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py",  line 175, in execute
self.error_handler.check_response(response)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 166, in check_response
raise exception_class(message, screen, stacktrace)
NoSuchElementException: Message: Unable to locate element:  {"method":"id","selector":"testcore"}

My full code snipped it as follows: 我的完整代码如下:

    # This class is where all my locators will be defined
from selenium.webdriver.common.by import By

class MainPageLocators(object):
    GO_BUTTON = (By.ID, 'submit')
    usernameTxtBox = (By.ID, 'unid')
    passwordTxtBox = (By.ID, 'pwid')
    submitButton = (By.ID, 'button')
    AdministrationButton = (By.CSS_SELECTOR, 'div.gwt-HTML.firepath-matching-node')
    AdministrationButtonXpath = (By.XPATH, '/body/div[2]/div[2]/div/div[2]/div/div[2]/div/div[7]/div/div')
    AdministrationButtonCSS = (By.CSS_SELECTOR, '/body/div[2]/div[2]/div/div[2]/div/div[2]/div/div[7]/div/div')
    contentFrame = (By.ID, 'testcore')


from element import BasePageElement
from locators import MainPageLocators

# This class is the base page class
class BasePage(object):

def __init__(self, driver):
    self.driver = driver

# This class is the LoginPage.  All the methods for the login page are defined here
class LoginPage(BasePage):

def click_go_button(self):
    element = self.driver.find_element(*MainPageLocators.GO_BUTTON)
    element.click()

def userLogin_valid(self):
    userName_textbox = self.driver.find_element(*MainPageLocators.usernameTxtBox)
    userName_textbox.clear()
    userName_textbox.send_keys("user1")
    password_textbox =  self.driver.find_element(*MainPageLocators.passwordTxtBox)
    password_textbox.clear()
    password_textbox.send_keys("Pass1")
    submitButton = self.driver.find_element(*MainPageLocators.submitButton)
    submitButton.click()

# Is the Administration button present on the dashboard page
def isAdministration_present(self):

    content_frame = self.driver.find_element(*MainPageLocators.contentFrame)
    self.driver.switch_to.frame(content_frame)
    administrationButtonCSS = self.driver.find_element(*MainPageLocators.AdministrationButtonCSS)



    # This class is the TestCase test class for the Login page where all the test cases is defined 
    import unittest
    from selenium import webdriver
    import page

    class LoginPage_TestCase(unittest.TestCase):

def setUp(self):
    self.driver = webdriver.Firefox()
    self.driver.get("http://mypc:8080/testcore01")

def test_login_valid_user(self):
    login_page = page.LoginPage(self.driver)
    login_page.userLogin_valid()
    login_page.isAdministration_present()

When i inspect the element in Firefox the HTML is as follows: 当我检查Firefox中的元素时,HTML如下:

    <body style="margin: 0px;">
    <iframe id="__gwt_historyFrame" style="position: absolute; width: 0;   height: 0; border: 0;" tabindex="-1" src="javascript:''"/>
    <iframe id="testCore" src="javascript:''" style="position: absolute; width: 0px; height: 0px; border: medium none;" tabindex="-1"/>
   <div style="position: absolute; z-index: -32767; top: -20cm; width: 10cm; height: 10cm; visibility: hidden;" aria-hidden="true"/>
   <div class="gwt-TabLayoutPanelTab GEGQEWXCK" style="background-color: rgb(254, 255, 238);">
   <div class="gwt-TabLayoutPanelTabInner">
   <div class="gwt-HTML">Administration</div>

You have a typo in your selector. 您的选择器中有一个错字。

contentFrame = (By.ID, 'testcore')

should be 应该

contentFrame = (By.ID, 'testCore') # capital "C"

Why? 为什么?

According to your HTML: 根据您的HTML:

<iframe id="testCore"...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM