Home > Software design >  Selenium -For Loops
Selenium -For Loops

Time:04-17

I'm not a programmer but I'm trying to learn some things that will help me with my tasks in my job I am trying to work with the loops and I want from my code

*If the element does not exist on the page, then I want the process to be skipped to the next line from my excel sheet ..

  ReID=ws1.cell(row=i, column=int(1)).value
  REG1="//a[contains(., 'Details') and contains(@href," str(ReID) ")]"
  WebDriverWait(driver, 20).until(
  EC.element_to_be_clickable((By.XPATH, REG1))).click()

My Full code :-

 path1 = r"C:\Users\ahmedb\Downloads\COURSE_REGISTRATION.xlsx"
 wb1 = openpyxl.load_workbook(path1) # this will open your excel file
 ws1 = wb1.worksheets[0] # this will open your excel sheet1
 LastRowCount = ws1.max_row # this will find max row from the excel
  start_row_number = 2

  for i in range(int(start_row_number), int(LastRowCount)):

  ReID=ws1.cell(row=i, column=int(1)).value  


  REG1="//a[contains(., 'Details') and contains(@href," str(ReID) ")]"
  WebDriverWait(driver, 20).until(
  EC.element_to_be_clickable((By.XPATH, REG1))).click() 

#If the above element on the page - .click() and continue with the code

#If the above element is not the page - Stop and re-search on the next line of

ReID=ws1.cell

The following line of code should run if the above element is available

  select = Select(driver.find_element_by_id('m_m_cBody_bdy_tbl_p_e_status'))
  select.select_by_visible_text('Accepted and Course not ended')
  select.select_by_value('Accepted')
  time.sleep(1)

  s = driver.find_element_by_xpath("//*[text()='Save']")
  driver.execute_script("arguments[0].click();",s)

  time.sleep(2)
  driver.quit()

CodePudding user response:

to check an element exist use the if condition as if driver.find_element_by_xpath(REG1)!=null

below should be your full code

 WebDriverWait(driver, 20).until(
      EC.element_to_be_clickable((By.XPATH, REG1)))
    if driver.find_element_by_xpath(REG1)!=null
    find_element_by_xpath(REG1).click()
    select = Select(driver.find_element_by_id('m_m_cBody_bdy_tbl_p_e_status'))
      select.select_by_visible_text('Accepted and Course not ended')
      select.select_by_value('Accepted')
      time.sleep(1)
    
      s = driver.find_element_by_xpath("//*[text()='Save']")
      driver.execute_script("arguments[0].click();",s)
    
      time.sleep(2)
      driver.quit()
  • Related