0

http://architects-register.org.uk/list/regions

So i have this website which i want to be able to have the code click the button A, then B and C and so on. How should i do that. I tried this code which tries to get the value of the child of the element then click the buttons accordingly but it does not work. I suspect there's some other issues aside the error below.

from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Firefox()

url = 'http://architects-register.org.uk/'
driver.get(url)

find_uk = driver.find_element_by_id('ctl00_hlCountiesT').click()

elements = alphabets[0].find_element_by_tag_name('li')

def getChild(el):
   return el.children[0].children[0]

for element in elements:
    element = element[elements]
    (getChild(element)).click()

NameError: name 'alphabets' is not defined

DebanjanB
  • 118,661
  • 30
  • 168
  • 217
Shaun
  • 53
  • 9

3 Answers3

1

alphabets isn't being imported or defined anywhere in the given code block, so trying to access an index on it will fail in the way shown. It looks like alphabets should probably be the result of calling find_elements_by_tag_name or find_elements_by_class_name which would return a list of your buttons which you could then iterate over and click on.

Daniel Samuels
  • 816
  • 8
  • 24
1

Induce WebDriverWait and visibility_of_all_elements_located() and then click on each link.

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver

driver = webdriver.Firefox()
url = 'http://architects-register.org.uk/'
driver.get(url)
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.ID,"ctl00_hlCountiesT"))).click()
elements =WebDriverWait(driver,10).until(EC.visibility_of_all_elements_located((By.XPATH,"//div[@class='alphabet_list_content']//li//input")))
for ele in range(len(elements)):
    elements = WebDriverWait(driver,10).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[@class='alphabet_list_content']//li//input")))
    elements[ele].click()
KunduK
  • 26,790
  • 2
  • 10
  • 32
0

To click on the button A, then B and C and so on you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    driver.get("http://architects-register.org.uk/list/regions")
    for i in range(1, len(WebDriverWait(driver, 5).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "div.alphabet_list_content>ul li"))))):
        WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.alphabet_list_content>ul li:nth-of-type({})>a>input".format(i)))).click()
    driver.quit()   
    
  • Using XPATH:

    driver.get("http://architects-register.org.uk/list/regions")
    for i in range(1, len(WebDriverWait(driver, 5).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[@class='alphabet_list_content']/ul//li"))))):
        WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='alphabet_list_content']/ul//following-sibling::li[{}]/a/input".format(i)))).click()
    driver.quit()   
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
DebanjanB
  • 118,661
  • 30
  • 168
  • 217