Arshad
Arshad

Reputation: 53

Click on submit button which does not have an id using selenium

I have tried to click on the submit button using on submit function but I can't see the result. The website I am trying to scrape is

Jntuh

The submit button has the following properties

<input type="submit" value="Submit">

I have used this .execute script

Resultbrowser.execute_script("""document.getElementById("myForm").onsubmit();""")

I dont know if the website has an on submit function

Entire code:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

link = 'http://results.jntuh.ac.in/jsp/SearchResult.jsp?degree=btech&examCode=1442&etype=r17&type=intgrade'
hallticket = "1780XXXXXX"
dobval = '1998-03-01'
with webdriver.Chrome() as Resultbrowser:

    Resultbrowser.get(link)
    Hallfield = Resultbrowser.find_element_by_name("htno")
    Hallfield.send_keys(hallticket)
    Hallfield.send_keys(Keys.RETURN)
    dob = Resultbrowser.find_element_by_class_name('hasDatepicker')
    dob.send_keys(dobval)
    dob.send_keys(Keys.RETURN)
    dob.send_keys(Keys.TAB)
    time.sleep(2)
    Resultbrowser.execute_script("""
    document.querySelector("#txtInput").value = document.querySelector("#txtCaptcha").value
    """)
    time.sleep(5)
    Resultbrowser.execute_script("""document.getElementById("myForm").onsubmit();""")

    time.sleep(10)

Upvotes: 0

Views: 60

Answers (1)

Sebastian Baltser
Sebastian Baltser

Reputation: 758

You are almost there. Locate the entire form-element by its id, which is myForm, and submit it using the .submit-method:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

link = 'http://results.jntuh.ac.in/jsp/SearchResult.jsp?degree=btech&examCode=1442&etype=r17&type=intgrade'
hallticket = "1780XXXXXX"
dobval = '1998-03-01'
with webdriver.Chrome() as Resultbrowser:

    Resultbrowser.get(link)
    Hallfield = Resultbrowser.find_element_by_name("htno")
    Hallfield.send_keys(hallticket)
    Hallfield.send_keys(Keys.RETURN)
    dob = Resultbrowser.find_element_by_class_name('hasDatepicker')
    dob.send_keys(dobval)
    dob.send_keys(Keys.RETURN)
    dob.send_keys(Keys.TAB)
    time.sleep(2)
    Resultbrowser.execute_script("""
    document.querySelector("#txtInput").value = document.querySelector("#txtCaptcha").value
    """)
    time.sleep(5)

    form = Resultbrowser.find_element_by_id("myForm")
    form.submit()

    time.sleep(10)

or using the .execute_script-method that you already wrote:

Resultbrowser.execute_script("""document.getElementById("myForm").submit();""")

i.e. replace onsubmit with submit.

Upvotes: 1

Related Questions