Feb-19-2019, 03:35 PM
As I can build a simple python script to perform automate process on some website, by using the webdriver.
as my simple python script, basically is using like .find_element_by_id, .find_element_by_name, time.sleep(5), etc.
But somehow it might raise some issue when the page is late to finish load, the HTML elements are might not able to located.
By Google Search, some people are using like this and I feel it is much more better because the progress will wait until finish load:-
Maybe there is some tutorial & explanation website that can let me have a better understanding regarding this
Thanks.
as my simple python script, basically is using like .find_element_by_id, .find_element_by_name, time.sleep(5), etc.
But somehow it might raise some issue when the page is late to finish load, the HTML elements are might not able to located.
By Google Search, some people are using like this and I feel it is much more better because the progress will wait until finish load:-
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support import expected_conditions as EC # if chromedriver is not in your path, you’ll need to add it here driver = webdriver.Chrome('chromedriver') driver.get("https://logon.rhb.com.my/") ''' Inintial web driver wait ''' wait = WebDriverWait(driver, 10) ''' The item we are going to access are under an Iframe, So we need to use the driver to enter the iframe by ID. And wait for it to finished load. ''' iframe = wait.until(EC.presence_of_element_located((By.ID, "ibkFrame"))) ''' Once the iframe is finish load switch the driver to it. ''' driver.switch_to.frame(iframe) ''' Get the item from id ''' txtAccessCode = wait.until(EC.visibility_of_element_located((By.ID, "txtAccessCode"))) ''' Print the Item. ''' txtAccessCode.send_keys("abc") btnLogin = wait.until(EC.visibility_of_element_located((By.ID, "cmdLogin"))) btnLogin.click()So my concern is
- when to use visibility_of_element_located, presence_of_element_located, and any others.
- What if the element have no ID or Name, what should I do?
Maybe there is some tutorial & explanation website that can let me have a better understanding regarding this
Thanks.