Selenium help on switching to iframe then click on link

Viewed 44

I am currently trying to click on each link which lead give me more info on the restaurants. https://www.halton.ca/For-Residents/Food-Safety/Dinewise/Search-Directory-of-Food-Premises-Dinewise.

The first restaurant is 151 Social and I am trying to click on it but doesn't seem to be working for me. I already compiled a pd.Series consisting of the xpath links to each restaurant

//*[@id="lblContent"]/div[3]/table/tbody/tr[1]/td[1]/a

That is just for the first restaurant

for element in range(len(df['Temp xPath links'])):    
    frame = WebDriverWait(driver, 30).until(EC.frame_to_be_available_and_switch_to_it((By.ID,"iframe-form")))
    ref = WebDriverWait(driver, 30).until(EC.presence_of_element_located(By.xpath(df['Temp xPath links'][element])))
    driver.execute_script("arguments[0].scrollIntoView();", ref)
    driver.execute_script("arguments[0].click();", ref)

I'm struggling to enter to the next page. Any help will be great.

1 Answers

Induce WebdriverWait() and wait for element_to_be_clickable() and XPATH

WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//a[text()='151 Social']"))).click()

Full Code:

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

driver = webdriver.Chrome()
driver.get("https://www.halton.ca/For-Residents/Food-Safety/Dinewise/Search-Directory-of-Food-Premises-Dinewise")
WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it((By.ID,"iframe-form")))
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.ID,"btnOK"))).click()
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.ID,"btnGetEstablishments"))).click()
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//a[text()='151 Social']"))).click()
Related