Electron microscopy
 
Find Elements on a Webpage
- Integrated Circuits -
- An Online Book -
Integrated Circuits                                                                                   http://www.globalsino.com/ICs/        


Chapter/Index: Introduction | A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | Appendix

=================================================================================

Steps to find elements on a webpage:

         i) Find the selector or locator information for those elements of interest, for instance, whith " Inspect" function on Chrome such as page4491.

         ii) Information can be used as elements are:
              ii.a) HTML tag.
              ii.b) Defined attribute.
              ii.c) Values for the attributes.
              ii.d) Structure of the page.

         iii) Tell Selenium how to find a particular element or set of elements on a web page programmatically and simulate user actions on these elements by passing the information which is found in the step above.
              The formats are:
              ID = "id"
              XPATH = "xpath"
              LINK_TEXT = "link text"
              PARTIAL_LINK_TEXT = "partial link text"
              NAME = "name"
              TAG_NAME = "tag name"
              CLASS_NAME = "class name"
              CSS_SELECTOR = "css selector"

              iii.a) find_element(By.ID, ""). This returns all the elements that have the same ID attribute values.

                 Example A: Use the code Switch_to function and alert popup windowbased on (page4482):
         Switch into frame layer by layer
                 Example B: Use the code Switch_to function and alert popup windowbased on (page4482):
         Switch into frame layer by layer
                 Example C: Use the code Switch_to function and alert popup windowbased on (page4482):
         Switch_to function and alert popup window

              iii.b) find_element(By.XPATH, "").

                 Example A: Use the code Switch_to function and alert popup windowbased on (page4482):
                 Switch_to function and alert popup window

                 Example B: Use the code Switch_to function and alert popup windowbased on (page4482):
         Switch into frame layer by layer

               Example C
          Switch_to function and alert popup window (page4479)
        Righ click the line above:  
          Switch_to function and alert popup window  

               Example D:  (page4491)
          Upload Files to Webpages
          Upload Files to Webpages

               Example E:  (page4491) Upload files by using Selenium (always use "input" for "send_key"):         
         i) Go to the website.
         ii) Right click "Upload File"
          Upload Files to Webpages
          This case is more complicated: need to scroll down and trace the code (see image and the obtained "layers" below), and then find "input ..." and "type = "file" " there is the most important thing, where the location the file will be sent.
         Upload Files to Webpages
          The "layers" give:
             "//div[@id='uploader_buttons']/div/input"
          And, the submission will be the same if there is one.
          Upload Files to Webpages
Code:
          Upload Files to Webpages
Output:         
          Upload Files to Webpages
          Upload Files to Webpages
          Upload Files to Webpages

               Example F:  (page4491) Upload files by using Selenium (always use "input" for "send_key"):    
          Upload Files to Webpages
Code:
          Upload Files to Webpages

               Example G :  (page4491)

Upload files with full XPATH when there are "multiple id":    
         Upload Files to Webpages
         Upload Files to Webpages
         Upload Files to Webpages
         Upload Files to Webpages
Then, the full XPATH is obtained: /html/body/form/div[3]/div[1]/section[2]/div/div[2]/div[2]/div[4]/div/label[1]/input.    
Code
:
          Upload Files to Webpages
Output:         
          Upload Files to Webpages

 


       Once the line below is performed, then the second window will open:
              driver.find_element(By.XPATH, "//*[@id='Tabbed']/a/button").click()

              iii.c) Find element(s) by their HTML tag name. find_element(By.TAG_NAME, ""). This is not a reliable way since this only selects the first tag in the webpage. However, there can be many tags with the same tage name. Code:
                   
from selenium import webdriver
                   
from selenium.webdriver.common.by import By
                   
from selenium.webdriver.chrome.service import Service
                   
import time

                   ser = Service(r"C:\usr\local\bin\chromedriver.exe")
                   
driver = webdriver.Chrome(service=ser)
                   
time.sleep(2)
                   
driver.implicitly_wait(3)
                   
driver.maximize_window()
                   
time.sleep(2)

                   driver.get("https://secure.yatra.com/social/common/yatra/signin.htm")
                   
time.sleep(3)
                   
driver.find_element(By.TAG_NAME, "input").send_keys("yougui.liao@gmail.com")

              iii.d) find_element(By.CLASS_NAME, ""). Code:
                   
from selenium import webdriver
                   
from selenium.webdriver.common.by import By
                   
from selenium.webdriver.chrome.service import Service
                   
import time

                   ser = Service(r"C:\usr\local\bin\chromedriver.exe")
                   
driver = webdriver.Chrome(service=ser)
                   
time.sleep(2)
                   
driver.implicitly_wait(3)
                   
driver.maximize_window()
                   
time.sleep(2)

                   driver.get("https://secure.yatra.com/social/common/yatra/signin.htm")
                   
time.sleep(3)
                   # Wrong since it includes all the classes seperated by spaces
                   # driver.find_element(By.CLASS_NAME, "yt-sme-mobile-input required_field email-login-box").send_keys("yougui.liao@gmail.com")
                   
driver.find_element(By.CLASS_NAME, "email-login-box").send_keys("yougui.liao@gmail.com")

                   find_element(By.CLASS_NAME, "").
                   find_element(By.CLASS_NAME, "").            
         

              iii.e) find_element(By.LINK_TEXT, ""). Code:          
                     find_element(By.CLASS_NAME, "").
                     find_element(By.CLASS_NAME, "").
Output:                     
                     find_element(By.CLASS_NAME, "").

              iii.f) Partial Link Text by using the text which is not dynamic. find_element(By.PARTIAL_LINK_TEXT, ""). Code:          
                     find_element(By.CLASS_NAME, "").
                     find_element(By.CLASS_NAME, "").
Output:                     
                     find_element(By.CLASS_NAME, "").

              iii.g) CSS.SELECTOR. find_element(By.CSS.SELECTOR, ""). Code:          
                     find_element(By.CLASS_NAME, "").
                     find_element(By.CLASS_NAME, "").Output:                     
                     find_element(By.CLASS_NAME, "").

============================================

         
         
         
         
         
         
         
         
         
         
         
         
         
         
         
         
         
         

 

 

 

 

 

 

 

 

 

 

=================================================================================