Python Selenium - Wait until Element is loaded
To locate the element you need to induce WebDriverWait for the visibility_of_element_located() and you can use the following Locator Strategies:
XPATH:
element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//*[@id="ipv4_info"]/span[3]")))
CSS_SELECTOR:
element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "#ipv4_info span:nth-child(3)")))
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Wait until element is fully loaded using selenium - python
Try this:
root_elm = driver.find_elements_by_xpath(xpath)
important_elm = WebDriverWait(driver, 10).until(
EC.presence_of_element_located(By.CLASSNAME, "market-title-v3"))
Python + Selenium: Wait until element is fully loaded
First of all I strongly believe you were pretty close. You simply need to format your code in a Pythonic which may solve your issue straight away as follows :
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="accountStandalone"]/div/div/div[2]/div/div/div[1]/button'))).click()
You have pulled a rug over the actual issue by mentioning it doesn't wait until it found but goes instant and does other stuff which it shouldn't rather than mentioning what your program is supposed to do (e.g. your code trials) and what wrong your program is doing (i.e. error stack trace).
As per the HTMLs you have shared you can induce a waiter for either of the WebElements as follows :
Waiter for the visibility of the text NU ÄR DU MEDLEM, Hello. :
CSS_SELECTOR :
WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.confirmation-title.nsg-font-family--platform.nsg-text--black.edf-title-font-size--xlarge.js-confirmationTitle")))
XPATH :
WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='confirmation-title nsg-font-family--platform nsg-text--black edf-title-font-size--xlarge js-confirmationTitle' and contains(.,'NU ÄR DU MEDLEM, Hello.')]")))
Waiter for the button with text FORTSÄTT :
CSS_SELECTOR :
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.nsg-button.nsg-bg--black.register-next-step-cta.js-nextStepCta")))
XPATH :
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='nsg-button nsg-bg--black register-next-step-cta js-nextStepCta' and contains(.,'FORTSÄTT')]")))
Wait until page is loaded with Selenium WebDriver for Python
The webdriver
will wait for a page to load by default via .get()
method.
As you may be looking for some specific element as @user227215 said, you should use WebDriverWait
to wait for an element located in your page:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
browser = webdriver.Firefox()
browser.get("url")
delay = 3 # seconds
try:
myElem = WebDriverWait(browser, delay).until(EC.presence_of_element_located((By.ID, 'IdOfMyElement')))
print "Page is ready!"
except TimeoutException:
print "Loading took too much time!"
I have used it for checking alerts. You can use any other type methods to find the locator.
EDIT 1:
I should mention that the webdriver
will wait for a page to load by default. It does not wait for loading inside frames or for ajax requests. It means when you use .get('url')
, your browser will wait until the page is completely loaded and then go to the next command in the code. But when you are posting an ajax request, webdriver
does not wait and it's your responsibility to wait an appropriate amount of time for the page or a part of page to load; so there is a module named expected_conditions
.
Make Selenium wait until text is available
To locate a visible element instead of presence_of_element_located() you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategy:
driver.get(url)
try:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "/html/body/app-root/app-wrapper/div/div[2]/app-etp/div[2]/div[3]/div[2]/div/div[2]/app-widget-quote-box/div/div/table/tbody/tr[3]/td[1]"))).text)
finally:
pass
Related Topics
What Do I Use on Linux to Make a Python Program Executable
Cqlsh Connection Error: 'Ref() Does Not Take Keyword Arguments'
Linux Command-Line Call Not Returning What It Should from Os.System
Uninstall Python Built from Source
Set Chrome Browser Binary Through Chromedriver in Python
Fail During Installation of Pillow (Python Module) in Linux
Get Total Physical Memory in Python
In Python Script, How to Set Pythonpath
Python Multiprocessing: Permission Denied
Django Server Killed Frequently
How to Install Pyodbc on Linux
A Weighted Version of Random.Choice
What's the Canonical Way to Check For Type in Python
Difference Between Shallow Copy, Deepcopy and Normal Assignment Operation
Interactively Validating Entry Widget Content in Tkinter