i am trying to submit the form using selenium:
<!DOCTYPE html>
<html>
<head>
<title>Insert New bids</title>
</head>
<body>
<form method="post" action="home/" id="bids-form">
{% csrf_token %}
<label for="fname">Bid number:</label><br>
<input type="text" id="bid" name="bid"><br>
<label for="lname">Department Name:</label><br>
<input type="text" id="dname" name="dname"><br>
<label for="fname">Address:</label><br>
<input type="text" id="address" name="address"><br>
<label for="lname">services:</label><br>
<input type="text" id="services" name="services"><br>
<label for="fname">start date:</label><br>
<input type="text" id="sdate" name="sdate"><br>
<label for="lname">start time:</label><br>
<input type="text" id="stime" name="stime"><br>
<label for="lname">End Date:</label><br>
<input type="text" id="edate" name="edate"><br>
<label for="lname">End time:</label><br>
<input type="text" id="etime" name="etime"><br>
<label for="lname">Pdf link:</label><br>
<input type="text" id="pdflink" name="pdflink"><br>
<label for="lname">location:</label><br>
<input type="text" id="location" name="location"><br>
<button type="submit">Save</button>
</form>
</body>
</html>
and here is my code of inserting data through selenium:
driver.execute_script('''window.open("http://127.0.0.1:8000/insert/","_blank");''')
print(driver.window_handles)
window_after = driver.window_handles[3]
driver.switch_to.window(window_after)
driver.find_element(By.XPATH,"/html/body/form/input[2]").send_keys(Bid_number)
print("line 1")
driver.find_element(By.XPATH,"/html/body/form/input[3]").send_keys(DepartmentName)
print("line 2")
driver.find_element(By.XPATH,"/html/body/form/input[4]").send_keys(Address)
print("line 3")
driver.find_element(By.XPATH,"/html/body/form/input[5]").send_keys(manpower_text)
print("line 4")
driver.find_element(By.XPATH,"/html/body/form/input[6]").send_keys(start_time[0])
print("line 5")
driver.find_element(By.XPATH,"/html/body/form/input[7]").send_keys(start_time[1])
print("line 6")
driver.find_element(By.XPATH,"/html/body/form/input[8]").send_keys(end_time[0])
print("line 7")
driver.find_element(By.XPATH,"/html/body/form/input[9]").send_keys(end_time[1])
print("line 8")
driver.find_element(By.XPATH,"/html/body/form/input[10]").send_keys(pdfHref)
print("line 9")
driver.find_element(By.XPATH,"/html/body/form/input[11]").send_keys(location)
print("line 10")
time.sleep(5)
# driver.find_element(By.XPATH,"/html/body/form/button").click
driver.close()
driver.switch_to.window(driver.window_handles[2])
the code runs till line 2 and after that is autosubmits the form and i dont know why that is happening.
Note: Values are not null all the varibles have value after line 2 there is no excution of code.
can someoneplease help <3
CodePudding user response:
Auto-submission might happen when some value contains the new-line char, and while it tries to sendKeys, it presses the ENTER and so the form submitted.
I've checked this hypothesis:
driver.get("https://www.w3schools.com/html/html_forms.asp")
fname = driver.find_element(By.XPATH("//input[@id='fname']"))
fname.clear()
fname.send_keys('Some Fname')
lname = driver.find_element(By.XPATH("//input[@id='lname']"))
lname.clear()
lname.sendKeys('Some \nLname')
and the next data was submitted:
fname=Some Fname&lname=Some
so, double-check your input data DepartmentName or Address fields.
Replace all line-breaks with a space:
Address = Address.replace('\n', ' ').replace('\r', '')
CodePudding user response:
Just add "required" arguments in the input tag of the html and you good to go
<input type="text" id="address" name="address" required><br>
