I'm trying to write a script that will auto-fill an online form with python and selenium, but I'm having trouble grabbing an element from the html code.
This is my starting code. Everything works until I try to identify the element. The element's id changes for every new instance of the website, so I'm trying a workaround which I believe grabs the correct id for each instance:
from selenium import webdriver
from selenium.webdriver.common.by import By
import requests
web = webdriver.Chrome()
url = 'https://lincdoc.ou.edu/lincdoc/doc/run/ouathletics/OU_AdvisingForm2#ldTimeoutUri'
web.get(url)
html_text = requests.get(url).text
instance_id_index = html_text.index("<div id=")
instance_id = html_text[instance_id_index+9] + html_text[instance_id_index+10] + html_text[instance_id_index+11] + html_text[instance_id_index+12]
student_id = "ID NUMBER"
student_id_box_id = instance_id + "q4"
I've tried the following, which all come back with a no such element exception:
(1) tried and failed to grab student id textbox element by id
web.find_element(By.ID, student_id_box_id).send_keys(student_id)
(2) tried and failed to grab student id textbox element by xpath
student_id_box_xpath = '//*[@id="' + instance_id + 'q4"]'
web.find_element(By.XPATH, student_id_box_xpath).send_keys(student_id)
(3) tried and failed to grab student id textbox element by full xpath
student_id_box_xpath = '//*[@id="' + instance_id + 'q4"]'
student_id_box_full_xpath = student_id_box_xpath + '/html/body/div/div[2]/div/div[2]/div[2]/div[3]/div/div[3]/table/tbody[2]/tr[1]/td[3]/div/input'
web.find_element(By.XPATH, student_id_box_full_xpath).send_keys(student_id)
Can anyone shed some light on this? I'm very very new to programming and python and it took awhile for me to get even this far. Thanks in advance!