I'm filling two forms that has different urls using selenium python. The script fills the first form and click on a submit button to go to the second form. Sometimes, It does not go to the second form, it stays on the first one.
What I want to do is to check if the url stays the same after clicking on the submit button, the script redo the process and refill the first form again, if the url changes, the script continue working normally.
This is what I tried so far
first_url = 'http://example.com/1'
check = driver.current_url
while True:
if first_url != check:
break
else:
form1() # function that fills the first form
The problem I am facing is that the first form continue to fills after that, the script doesn't skip the check and moves to the next functions. I want to do only one check that's all.
Code of the first form
def form1():
try:
driver.get(site)
mission = WebDriverWait(driver, 10).until(ec.element_to_be_clickable((By.XPATH, '//*[@id="mission"]/option[1]'))).click()
service = WebDriverWait(driver, 10).until(ec.element_to_be_clickable((By.XPATH, '//*[@id="service"]/option[4]'))).click()
phone = WebDriverWait(driver, 10).until(ec.element_to_be_clickable((By.XPATH, '//*[@id="phone"]'))).send_keys(' 15041245829')
email = WebDriverWait(driver, 10).until(ec.element_to_be_clickable((By.XPATH, '//*[@id="email"]'))).send_keys('[email protected]')
submit = driver.find_element('id','submit').click()
except Exception as e:
print(e)
Code of the second form
def form2():
try:
date = WebDriverWait(driver, 10).until(ec.element_to_be_clickable((By.XPATH, '//*[@id="date"]/option[1]'))).click()
time = WebDriverWait(driver, 10).until(ec.element_to_be_clickable((By.XPATH, '//*[@id="time"]/option[2]'))).click()
submit = driver.find_element('id','submit').click()
except Exception as e:
print(e)
And I've added the check to a function
def check():
while True:
check = driver.current_url
if first_url != check:
break
else:
form1()
I execute the code like that:
form1()
check()
form2()
CodePudding user response:
update
form1->
def form1(site,driver):
try:
driver.get(site)
# since you never call these elements of the page there is no need to store them in variables
WebDriverWait(driver, 10).until(ec.element_to_be_clickable((By.XPATH, '//*[@id="mission"]/option[1]'))).click()
WebDriverWait(driver, 10).until(ec.element_to_be_clickable((By.XPATH, '//*[@id="service"]/option[4]'))).click()
WebDriverWait(driver, 10).until(ec.element_to_be_clickable((By.XPATH, '//*[@id="phone"]'))).send_keys(' 15041245829')
WebDriverWait(driver, 10).until(ec.element_to_be_clickable((By.XPATH, '//*[@id="email"]'))).send_keys('[email protected]')
submit = driver.find_elemnt(By.ID,'submit')
submit.click()
current_url = driver.current_url
if not check(site,current_url):
form1()
else:
return
except Exception as e:
print(e)
form2->
def form2(driver):
try:
# since you never call these elements of the page there is no need to store them in variables
WebDriverWait(driver, 10).until(ec.element_to_be_clickable((By.XPATH, '//*[@id="date"]/option[1]'))).click()
WebDriverWait(driver, 10).until(ec.element_to_be_clickable((By.XPATH, '//*[@id="time"]/option[2]'))).click()
driver.find_element('id','submit').click()
except Exception as e:
print(e)
check->
def check(url1,url2):
if url1 == url2:
return False
else:
return True
main->
site = 'https://www.example.com'
driver = webdriver.Chrome()
form1(site,driver)
form2(driver)
if this doesn't work then the issue is probably from the click on the submit button and without a link i cant help with that you have to keep testing yourself.