Python Selenium (waiting for frame, element lookups)

0 votes

I have these includes:

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys

Browser set up via

browser = webdriver.Firefox() 
browser.get(loginURL) 

However sometimes I do

browser.switch_to_frame("nameofframe")

And it won't work (sometimes it does, sometimes it doesn't).

I am not sure if this is because Selenium isn't actually waiting for pages to load before it executes the rest of the code or what. Is there a way to force a webpage load?

Because sometimes I'll do something like

browser.find_element_by_name("txtPassword").send_keys(password + Keys.RETURN)
#sends login information, goes to next page and clicks on Relevant Link Text
browser.find_element_by_partial_link_text("Relevant Link Text").click()

And it'll work great most of the time, but sometimes I'll get an error where it can't find "Relevant Link Text" because it can't "see" it or some other such thing.

Also, is there a better way to check if an element exists or not? That is, what is the best way to handle:

browser.find_element_by_id("something")

When that element may or may not exist?

Sep 14, 2018 in Selenium by Meci Matt
• 9,400 points
414 views

1 answer to this question.

0 votes

You could use WebDriverWait:

from contextlib import closing
from selenium.webdriver import Chrome as Browser
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import NoSuchFrameException


def frame_available_cb(frame_reference):
    """Return a callback that checks whether the frame is available."""
    def callback(browser):
        try:
            browser.switch_to_frame(frame_reference)
        except NoSuchFrameException:
            return False
        else:
            return True
    return callback

with closing(Browser()) as browser:
    browser.get(url)
    # wait for frame
    WebDriverWait(browser, timeout=10).until(frame_available_cb("frame name"))
answered Sep 14, 2018 by Martin
• 4,220 points

Related Questions In Selenium

0 votes
1 answer

Python + Selenium - Trying to find element by link containing certain words

2 issues. One thing is, "Catalogues" & ...READ MORE

answered Mar 27, 2018 in Selenium by nsv999
• 5,050 points
3,149 views
0 votes
2 answers

Unable to Click on an Element in Selenium (Python) even after finding it.

Here, I give you working script which ...READ MORE

answered Sep 19, 2018 in Selenium by Priyaj
• 56,160 points
5,834 views
0 votes
1 answer

Using gettext() method for the specific element using Selenium WebDriver

Mistake is that u r printing the ...READ MORE

answered Apr 10, 2018 in Selenium by Vardy
• 2,280 points
3,763 views
0 votes
1 answer

Searching for the element with   symbol in selenium

Use Below: //div[@class='Tips' and text()='\u00a0'] READ MORE

answered Apr 20, 2018 in Selenium by Vardy
• 2,280 points
1,162 views
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,050 points
248 views
0 votes
1 answer
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,130 points
619 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,130 points
235 views
0 votes
1 answer

Selenium with pyvirtualdisplay unable to locate element

If there is some dynamic content on ...READ MORE

answered Sep 14, 2018 in Selenium by Martin
• 4,220 points
188 views
0 votes
1 answer

How can I download the *.jar file from http:// seleniumhq.org using selenium WebDriver?

For Selenium Standalone Server use this: profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/java-archive"); and ...READ MORE

answered Apr 9, 2018 in Selenium by Martin
• 4,220 points
341 views