Python Forum

Full Version: drive wait in try
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello guys Im trying to click in a button in one webpage, but this button can have two different Xpath
/html/body/div[2]/main/div[1]/div/div/div/div/div/div[3]/div/section[8]/div/div[1]/div/form/input
/html/body/div[2]/main/div[1]/div/div/div/div/div/div[3]/div/section[9]/div/div[1]/div/form/input

try: 
		
		print("page loading")                                                       
		driver.wait.until(EC.presence_of_element_located((By.XPATH,"/html/body/div[2]/main/div[1]/div/div/div/div/div/div[3]/div/section[8]/div/div[1]/div/form/input")))
		driver.find_element_by_xpath("/html/body/div[2]/main/div[1]/div/div/div/div/div/div[3]/div/section[8]/div/div[1]/div/form/input").click()           
		driver.wait.until(EC.presence_of_element_located((By.NAME,"username")))
		driver.find_element_by_name("username").send_keys("")
		driver.wait.until(EC.presence_of_element_located((By.NAME,"password")))
		driver.find_element_by_name("password").send_keys("")
		print("loging")
		driver.wait.until(EC.presence_of_element_located((By.XPATH,"/html/body/div[2]/div/div[1]/login/div/login-form/div[1]/form/input")))
		driver.find_element_by_xpath("/html/body/div[2]/div/div[1]/login/div/login-form/div[1]/form/input").click()           
		
		
		print("next")
		print("2 sec")
		import time 
		time.sleep(2)

		driver.back()
		print("going back e i=i+1")
		i=i+1
	except NoSuchElementException as e:                
		driver.wait.until(EC.presence_of_element_located((By.XPATH,"/html/body/div[2]/main/div[1]/div/div/div/div/div/div[3]/div/section[9]/div/div[1]/div/form/input")))
		driver.find_element_by_xpath("/html/body/div[2]/main/div[1]/div/div/div/div/div/div[3]/div/section[9]/div/div[1]/div/form/input").click()           
		
		driver.wait.until(EC.presence_of_element_located((By.NAME,"username")))
		#driver.find_element_by_name("username").send_keys("")
		driver.wait.until(EC.presence_of_element_located((By.NAME,"password")))
		driver.find_element_by_name("password").send_keys(")
		print("loging")
		driver.wait.until(EC.presence_of_element_located((By.XPATH,"/html/body/div[2]/div/div[1]/login/div/login-form/div[1]/form/input")))
		driver.find_element_by_xpath("/html/body/div[2]/div/div[1]/login/div/login-form/div[1]/form/input").click()           


		print("2 sec")
		import time 
		time.sleep(2)

		driver.back()
		
		i=i+1
		
the problem is when the button have the 2nd option, I have always a error in

driver.wait.until(EC.presence_of_element_located((By.XPATH,"/html/body/div[2]/main/div[1]/div/div/div/div/div/div[3]/div/section[8]/div/div[1]/div/form/input")))
because he can not find...

the code don't jump for the
except NoSuchElementException as e:
and keep working...

do you know if I have a way to choose: the first Xpath appear click
between :

Xpath
/html/body/div[2]/main/div[1]/div/div/div/div/div/div[3]/div/section[8]/div/div[1]/div/form/input
/html/body/div[2]/main/div[1]/div/div/div/div/div/div[3]/div/section[9]/div/div[1]/div/form/input