-1

Selenium, cannot locate xPath element

I'm a beginner learning Selenium, trying to build a tiny web scraper. First part is to go to the site and click button "Search By Address". I copied the xPath from Inspecting the element in Google Chrome, however, using Selenium to find the element keeps throwing a "unable to locate" error.

from selenium import webdriver
web_Url = 'http://hcad.org/quick-search/'
driver = webdriver.Firefox(executable_path=r'C:\Users\Jaz\Documents\Python\Modules\geckodriver.exe')
driver.implicitly_wait(10) # this lets webdriver wait 10 seconds for the website to load
driver.get(web_Url)
button = driver.find_element_by_xpath("//*[@id='s_addr']").click()
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Jaz
  • 55
  • 13
  • Possible duplicate of [Select iframe using Python + Selenium](https://stackoverflow.com/questions/7534622/select-iframe-using-python-selenium) – JeffC Jan 31 '19 at 15:41

2 Answers2

1

The element you are trying to find is inside iframe. First, switch to iframe using 'driver.switchTo().frame()' command and then try to find the element.

More details on switching to iframe is discussed in this article.

from selenium import webdriver
web_Url = 'http://hcad.org/quick-search/'
driver = webdriver.Firefox(executable_path=r'C:\Users\Jaz\Documents\Python\Modules\geckodriver.exe')
driver.implicitly_wait(10) # this lets webdriver wait 10 seconds for the website to load
driver.get(web_Url)
element = driver.find_element_by_xpath("//div[@class='c-general']/iframe")
driver.switch_to_frame(element)
driver.find_element_by_xpath("//*[@id='s_addr']").click()
driver.switch_to_default_content()
arunkvelu
  • 1,631
  • 10
  • 21
  • Thank you for this code snippet. I'll have to look into iframe and studying it a bit more. I have a general understanding, thank you for the help – Jaz Jan 27 '19 at 15:57
0

To click() on the element with text as Search By Address as the the desired element is within an <iframe> so you have to:

  • Induce WebDriverWait for the desired frame to be available and switch to it.
  • Induce WebDriverWait for the desired element to be clickable.
  • You can use the following solution:

    • Code Block:

      from selenium import webdriver
      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.support import expected_conditions as EC
      from selenium.webdriver.common.by import By
      
      driver = webdriver.Firefox(executable_path=r'C:\Utility\BrowserDrivers\geckodriver.exe')
      driver.get("http://hcad.org/quick-search/")
      WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@src='https://public.hcad.org/records/quicksearch.asp']")))
      WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#s_addr"))).click()
      
  • Browser Snapshot:

address_search

Here you can find a relevant discussion on Ways to deal with #document under iframe

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352