1

First post on here and brand new to Python. I am trying to learn how to scrape data from a website. When you first load the website, a disclaimer window shows up and all I am trying to do is hit the accept button using the browser.find_element_by_id.

I am using the webdriverwait command to wait for the page to load prior to clicking the "Accept" button but I keep getting a Timeoutexception. Here is the code that I currently have:

from selenium import webdriver
#get the chrome webdriver path file
browser = webdriver.Chrome(executable_path=r"C:/Program Files (x86)/Google/Chrome/Application/chromedriver.exe")
browser.get('http://foreclosures.guilfordcountync.gov/')

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
#wait until element is loaded
wait = WebDriverWait(browser, 10)
wait.until(EC.presence_of_element_located((By.ID, "cmdAccept")))

element = browser.find_element_by_id("cmdAccept")
element.click()

Here is the error I keep getting:

Traceback (most recent call last):
  File "C:/Users/Abbas/Desktop/Foreclosure_Scraping/Foreclosure_Scraping.py", line 33, in <module>
    wait.until(EC.presence_of_element_located((By.ID, "cmdAccept")))
  File "C:\Users\Abbas\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 

I believe it has something to do with the calling out the ID of the button itself from the website but I honestly do not know. Any help is greatly appreciated.

1 Answer 1

2

Your attempts to locate the element are unsuccessful because of they are nested within an iframe. One must tell selenium to switch to the iframe that contains the desired element before attempting to click it or use it in any way. Try the following:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
#get the chrome webdriver path file
browser = webdriver.Chrome(executable_path=r"C:/Program Files (x86)/Google/Chrome/Application/chromedriver.exe")
browser.get('http://foreclosures.guilfordcountync.gov/')
browser.switch_to.frame(browser.find_element_by_name("ctl06"))
wait = WebDriverWait(browser, 10)
wait.until(EC.presence_of_element_located((By.ID, "cmdAccept")))
element = browser.find_element_by_id("cmdAccept")
element.click()
Sign up to request clarification or add additional context in comments.

3 Comments

Nice answer. As OP is a new user, I just wanted to make the explanation as clear as possible.
I am facing the same issue I want to access an element but i think it is nested within iframe. <iframe scrolling="yes" src="goinet.in/gonoshow/default.aspx" style="border: 0px none; margin-left:10px;min-height: 700px; margin-top: 0px; width: 926px;">. I wanted to know how do I switch to this iframe. browser.switch_to.frame(browser.find_element(<what to put here>). As this iframe does not have id or name
I figured out how to switch the iframe, here is the code if anyone is looking for. iframe = browser.find_element(By.XPATH, "//iframe") browser.switch_to.frame(iframe) pnr = browser.find_element(By.ID, 'ContentPlaceHolder1_txt_PNR')

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.