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?
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.
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.
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.
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.
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")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With