Takeshi Patterson
Takeshi Patterson

Reputation: 1277

what is a browser variable in python?

I'm a newb trying to write a Selenium script using Python. There's a section of the script where I'm asking the program to wait until the iframe has appeared before selecting another element.

I've got and error:

NameError: global name 'browser' is not defined

I understand the nature of the problem - the variable needs to be defined before it can be used, but I don't know what to initialise the variable with!

I know this sounds like a strange question, as why would I use a variable that I haven't initialised. Problem is, that part of my code I took from a fix on here where the definition of the variable isn't given.

Any help much appreciated.

from selenium import webdriver 
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.support.wait import WebDriverWait
import unittest, time, re

class Gymboxtest3(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(5)
        self.base_url = "http://gymbox.com"
        self.verificationErrors = []
        self.accept_next_alert = True

    def test_gymboxtest3(self):
        driver = self.driver
        driver.get(self.base_url + "/Login")
        time.sleep(30)

        frame = WebDriverWait(browser, 30).until(lambda x: x.find_element_by_id("iframe"))
        browser.switch_to_frame(frame)

Upvotes: 0

Views: 2383

Answers (2)

thkang
thkang

Reputation: 11543

the error is at:

def test_gymboxtest3(self):
    driver = self.driver
    driver.get(self.base_url + "/Login")
    time.sleep(30)

    frame = WebDriverWait(browser, 30).until(lambda x: x.find_element_by_id("iframe"))
    browser.switch_to_frame(frame) # <-- here

maybe you've meant driver.switch_to_frame(frame)? you're using browser yourself, but you never defined it beforehand.

Upvotes: 1

Aaron Digulla
Aaron Digulla

Reputation: 328674

Replace browser with driver:

frame = WebDriverWait(driver, 30)...
driver.switch_to_frame(frame)

Upvotes: 0

Related Questions