Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wait for class to exist before continuing with selenium in Firefox

I'm trying to make selenium wait until a certain class is able to be found on a page, I've tried several bits of code but nothing is working

Trying the following:

while not firefox.find_element_by_css_selector('.but selected'):
    print "test"
    time.sleep(1)

Returns

selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element

Trying the following:

while not firefox.find_element_by_class_name('but selected'):
    print "test"
    time.sleep(1)

Returns:

selenium.common.exceptions.InvalidSelectorException: Message: The given selector but selected is either invalid or does not result in a WebElement. The following error occurred: InvalidSelectorError: Compound class names not permitted

Any idea what I am doing wrong and how to fix it?

like image 554
Ryflex Avatar asked May 20 '16 05:05

Ryflex


People also ask

How do I use Selenium to wait until?

We can wait until an element is present in Selenium webdriver. This can be done with the help of synchronization concept. We have an explicit wait condition where we can pause or wait for an element before proceeding to the next step. The explicit wait waits for a specific amount of time before throwing an exception.

How do you wait for an element to appear in Selenium?

Explicit Wait in SeleniumBy using the Explicit Wait command, the WebDriver is directed to wait until a certain condition occurs before proceeding with executing the code. Setting Explicit Wait is important in cases where there are certain elements that naturally take more time to load.

Is WebDriver wait a class in Selenium?

WebDriverWait Class. The WebDriverWait type exposes the following members. Initializes a new instance of the WebDriverWait class. Initializes a new instance of the WebDriverWait class.

What is default wait time for implicit wait in Selenium WebDriver?

WebDriver Implicit Wait Implicit waits are used to provide a default waiting time (say 30 seconds) between each consecutive test step/command across the entire test script. Thus, the subsequent test step would only execute when the 30 seconds have elapsed after executing the previous test step/command.


1 Answers

You could try explicit-waits. Here is a small example:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException


def test(url):
    wait_for_element = 30  # wait timeout in seconds
    firefox = webdriver.Firefox()
    firefox.get(url)

    try:
        WebDriverWait(firefox, wait_for_element).until(
            EC.element_to_be_clickable((By.CLASS_NAME, "but selected'")))
    except TimeoutException as e:
        print("Wait Timed out")
        print(e)

if __name__ == '__main__':
    test("http://www.python.org")
like image 139
salomonderossi Avatar answered Oct 06 '22 00:10

salomonderossi