1

In this section of my text, I am looping to select all check boxes across a number of pages (successful). When the next button is no longer enabled (and thus no more pages to turn, try command), I want to click the apply button. There are two different apply buttons, a visible and invisible one. The way my text is written, I receive an element not interactable exception, and thus my timeout exception isn't seen through. That's unexpected because the button I am trying to click is clearly visible and clickable (manually).

Original website: https://icem.data-archive.ac.uk/#step2

Does anyone have an idea what could be causing this element not interactable? I have attached a photo of the HTML of the two applied buttons: https://i.stack.imgur.com/xBl9X.png

Note: introducing WebDriverWait on the exception command just induces another timeoutexception.

county_pop < 1000000:
               
       while True:

              checklist =  webD.find_elements_by_xpath('//*[@class="modal-content"]//input[@class="ng-pristine ng-valid"]')

                    for elem in checklist:
                         elem.click()

                    try:
                         WebDriverWait(webD, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[not(contains(@class, "disabled"))]/a[.="Next"]'))).click()

                    except TimeoutException:

                         webD.find_element_by_xpath('//div[@class="modal-content"]//div[@class="col-sm-3 text-left visible-xs"]/button').click()

Script until then:

import selenium
import time
from selenium import webdriver as wd
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import StaleElementReferenceException, NoSuchElementException, NoSuchWindowException
from selenium.webdriver.common.action_chains import ActionChains

chrome_path = r'C:\webdrivers\chromedriver.exe'

webD=wd.Chrome(executable_path=chrome_path)

webD.get('https://beta.ukdataservice.ac.uk/myaccount')

WebDriverWait(webD, 20).until(EC.element_to_be_clickable(
    (By.XPATH, '//*[@id="myaccount-login"]/div/div/div/div/div/div[2]/div/div/div/form/div/p/button'))).click()

WebDriverWait(webD, 20).until(EC.element_to_be_clickable(
    (By.XPATH, '//*[@id="username"]'))).send_keys('ukd1217078879')

WebDriverWait(webD, 20).until(EC.element_to_be_clickable(
    (By.XPATH, '//*[@id="password"]'))).send_keys('Census4Me!')

webD.find_element_by_xpath('/html/body/div/div/div[2]/div[1]/form/div[6]/button').click()

webD.get('https://icem.data-archive.ac.uk/#step1')

#YEARS LOOP BEGINS 

for year in [1851, 1861, 1881, 1891, 1901, 1911]:

     webD.find_element_by_xpath("//b[@class='ng-binding' and text()='{}']/preceding-sibling::input[@type='radio']".format(str(year))).click()

     # Continuing to England & Step 2, Opening Counties Variable, More Variables 

     WebDriverWait(webD, 20).until(EC.element_to_be_clickable((By.XPATH, '//b[@id = "country_england"]/preceding-sibling::input'))).click()

     webD.find_element_by_xpath('//html/body/div/section/section[1]/article[2]/div/div/button').click()

     WebDriverWait(webD, 20).until(EC.element_to_be_clickable(
    (By.XPATH, '//*[@id="county_category"]/button[1]'))).click()

     WebDriverWait(webD, 20).until(EC.element_to_be_clickable(
    (By.XPATH, '//*[@id="county_category"]/div[2]/div/div[2]/button'))).click()



# COUNTIES LOOP BEGINS 

     conto = 1 
     count = conto

     while count > 10: #Getting to the correct county page 
          webD_find_element_by_link_text('Next').click()
          count = count - 10

     for i in range(count,11):

          #Clicking correct county
          WebDriverWait(webD, 20).until(EC.element_to_be_clickable((By.XPATH, '/html/body/div[3]/div/div/div[3]/div[{}]/label/input'.format(count)))).click()                           
          #Adding 1 to absolute count, conto 
          conto = conto + 1
          #Store variables 
          county_text = webD.find_element_by_xpath('/html/body/div[3]/div/div/div[3]/div[{}]/label'.format(count)).text

          county_pop = int(county_text.split("(")[-1].replace(")","").replace(",", ""))

          print(county_pop)
         
          #Apply County Selection, open HISCO & More Variables
          WebDriverWait(webD, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[contains(.,'Apply')]"))).click()

          WebDriverWait(webD, 20).until(EC.presence_of_element_located((By.XPATH, '//span[@class = "ice-allow-download-alert ng-animate ng-hide-remove ng-hide-remove-active"]')))

          WebDriverWait(webD, 20).until(EC.invisibility_of_element_located((By.XPATH, '//span[@class = "ice-allow-download-alert ng-animate ng-hide-remove ng-hide-remove-active"]')))

          WebDriverWait(webD, 20).until(EC.presence_of_element_located((By.XPATH, '//b[text()[contains(.,"HISCO classified occupation")]]/..'))).click()

          WebDriverWait(webD, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="workhistoryunit_category"]/div[2]/div/div[2]/button'))).click()

          #HISCO OCCUPATION SELECTION LOOP

          if county_pop < 1000000:
               
               while True:

                    checklist =  webD.find_elements_by_xpath('//*[@class="modal-content"]//input[@class="ng-pristine ng-valid"]')

                    for elem in checklist:
                         elem.click()

                    try:
                         WebDriverWait(webD, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[not(contains(@class, "disabled"))]/a[.="Next"]'))).click()

                    except TimeoutException:

                         webD.find_element_by_xpath('//div[@class="modal-content"]//div[@class="col-sm-3 text-left visible-xs"]/button').click()
                         
Rye Rye
  • 57
  • 5

1 Answers1

0
 temp = True
 if county_pop < 1000000:
    while temp:

          checklist = webD.find_elements_by_xpath(
              '//*[@class="modal-content"]//input[@class="ng-pristine ng-valid"]')

          for elem in checklist:
                elem.click()

          try:
                WebDriverWait(webD, 10).until(EC.element_to_be_clickable(
                    (By.XPATH, '//*[not(contains(@class, "disabled"))]/a[.="Next"]'))).click()

          except:
                WebDriverWait(webD, 10).until(EC.element_to_be_clickable(
                    (By.XPATH, '//button[contains(text(),"Apply")]'))).click()
                temp = False

in while loop you had True which will make it run infinitly , change it to a varaible Temp and set it to false as soon as the next button is not clickable.

Also use webdriver wait and click on the first button

PDHide
  • 18,113
  • 2
  • 31
  • 46
  • Hi! Thanks for your answer, it still isn't clicking the Apply button... I get a time out exception, which I also experienced earlier when I used WebdriverWait in the exception. So clicking the Apply really is the issue here – Rye Rye Feb 09 '21 at 18:12
  • o tested it and it works fine the new error is from different page see the line where exception is thorwn – PDHide Feb 09 '21 at 18:15