Uncategorized

Selenium Python Tutorial Series #5 – Selecting Elements using Class Name, Name, Xpath, and Link Text

<html>

<body>
  <h1>First H1</h1>
  <h2>First H2</h2>
  <h2>Second H2</h2>
<br>
<a href="https://Google.com">This is a link partiallinktext</a></h1>
<br>
<div class="example-class">Example Class Contains This Data</div>
<br>
<div name="example-name">Example name Contains This Data</div>
<br>
<form action="/action_page.php">
  <label for="fname">First name:</label>
  <input id="fname" type="text" name="fname"><br><br>
  <label for="lname">Last name:</label>
  <input type="text" id="lname" name="lname"><br><br>
  <input type="submit" value="Submit">
</form>
</body>
</html>
#pre-requisites
#libraries
#chrome driver
#Loading our web page

import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service

#set location the location of the webdriver
s = Service('/usr/local/bin/chromedriver')

driver = webdriver.Chrome(service=s)
driver.get("http://192.168.64.2/Example.php")

input_fname = driver.find_element(by=By.ID, value="fname")

input_lname = driver.find_element(by=By.ID, value="lname")
time.sleep(4)
input_fname.send_keys("Jonathan")
time.sleep(4)
input_lname.send_keys("North")
time.sleep(4)

submit_button = driver.find_element(by=By.XPATH, value="/html/body/form/input[3]")
submit_button.click()

#/html/body/form/input[3]

#example_name = driver.find_element(by=By.XPATH, value="//*[@id='fname']")
#example_name.send_keys("I sent this!")