Use selenium to click link based on [python] input

I made a program with selenium to go to my schools website, login and get all the text using a CSS selector and print it. It then prints out all available exams. However, there is a download link next to each exam, and I am trying to create an input with an exam name, and then make a selenium click on that particular download button connected to that exam.

I don't know much about HTML, so I'll try to be as clear as I can.

Here's the code and some images:

from selenium import webdriver

browser = webdriver.Chrome('/usr/local/bin/chromedriver')
browser.get('https://minatentor.hv.se/minatentor/')

username = browser.find_element_by_css_selector('#userNameInput')
password = browser.find_element_by_css_selector('#passwordInput')

username.send_keys(r"my_username")
password.send_keys('my_password')

password.submit()

tentor = browser.find_element_by_css_selector('#examTable > tbody')
print(tentor.text)
browser.close() 

      

This is the look and feel of the website. I used a CSS selector to get this text and print it ( '#examTable > tbody'

).

This is what the website looks like.

This is what it prints to the shell.

enter image description here

This is what the inspector looks like.

enter image description here

I used a CSS selector <tbody>

to get all the text. Each <tr>...</tr>

is a line marked in the picture. "Hรคmta" means "Download" in Swedish, so it's a download button.

I want to be able to record input with the course name ("Kursnamn" in the picture) and make selenium by clicking the download button associated with that course name. Like this:

enter image description here

Is it possible? Hope I made it as clear as possible!

+3


source to share


1 answer


Couldn't verify the reason, doesn't have HTML, but looks something like this.

s = raw_input("Kursnamn?")
download_item = browser.find_element_by_xpath('//tr[contains(text(), '+s+')]//a[@href]')

      



Then

download_item.click()

      

+4


source







All Articles