Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Selenium Webdriver Automate
#1
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:-

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
  1. when to use visibility_of_element_located, presence_of_element_located, and any others.
  2. 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.
Reply
#2
You always want to avoid using time.sleep as pages may be different in loading times depending on a lot of factors. I appended to snippsat's tutorial here brushing on that.

(Feb-19-2019, 03:35 PM)gahhon Wrote: What if the element have no ID or Name, what should I do?
You can get it form other means. Name and ID are not the only ways to get specific elements, and there are more built-in methods than visibility_of_element_located

title_is
title_contains
presence_of_element_located
visibility_of_element_located
visibility_of
presence_of_all_elements_located
text_to_be_present_in_element
text_to_be_present_in_element_value
frame_to_be_available_and_switch_to_it
invisibility_of_element_located
element_to_be_clickable
staleness_of
element_to_be_selected
element_located_to_be_selected
element_selection_state_to_be
element_located_selection_state_to_be
alert_is_present
find_element_by_id
find_element_by_name
find_element_by_xpath
find_element_by_link_text
find_element_by_partial_link_text
find_element_by_tag_name
find_element_by_class_name
find_element_by_css_selector


find_elements_by_name
find_elements_by_xpath
find_elements_by_link_text
find_elements_by_partial_link_text
find_elements_by_tag_name
find_elements_by_class_name
find_elements_by_css_selector
You can find the definition of each expected support condition here.

more info:
https://selenium-python.readthedocs.io/waits.html
https://selenium-python.readthedocs.io/l...ments.html

And those are just the built-in ones. You can build further and make custom ones.
Recommended Tutorials:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Need help for script access via webdriver to an open web page in Firefox Clixmaster 1 1,214 Apr-20-2023, 05:27 PM
Last Post: farshid
  Problem with Selenium webdriver Fred 1 2,008 Jan-10-2022, 05:45 PM
Last Post: Larz60+
  How do I iterate over an array and perform actions using selenium chrome webdriver? master 0 2,386 Sep-14-2020, 05:28 AM
Last Post: master
  Which webdriver is required for selenium in Pydroid App Rahatt 1 6,242 Jul-31-2020, 01:39 AM
Last Post: Larz60+
  Log In Button Won't Click - Python Selenium Webdriver samlee916 2 3,770 Jun-07-2020, 04:42 PM
Last Post: samlee916
  Hyperlink Click is not working in Selenium webdriver rajeev1729 0 1,995 May-02-2020, 11:21 AM
Last Post: rajeev1729
  Selenium webdriver error WiPi 4 11,982 Feb-09-2020, 11:38 AM
Last Post: WiPi
  Can not point to Selenium Webdriver path for Python Jupyter Notebook on Azure dadadance 4 10,010 Jul-31-2019, 10:00 PM
Last Post: perfringo
  Can i use selenium to automate electron based desktop application UI dharmendraradadiya 0 2,874 Jul-22-2019, 01:20 PM
Last Post: dharmendraradadiya
  Selenium Webdriver Memory Problem? satbir129 2 6,770 Mar-01-2019, 04:17 AM
Last Post: satbir129

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020