1

Am incredibly new to Selenium. I'm trying to use it to 'fill out' a web form so that ultimately I can get the results as a dictionary in Python. So far I have

from selenium.webdriver.common.keys import Keys
from time import sleep

driver = webdriver.Chrome()
driver.get("https://my.crawley.gov.uk/en/service/check_my_bin_collection?accept=yes&consentMessageIds[]=24")
sleep(5)
assert "Check bin collections" in driver.title
elem = driver.find_element_by_id('PostcodeSearch')
elem.clear()
elem.send_keys("RH10 7AB")
elem.send_keys(Keys.RETURN)
driver.close()

but I'm struggling to get the element on the page no matter what method I choose

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

2 Answers2

1

Try below lines

driver.get("https://my.crawley.gov.uk/en/service/check_my_bin_collection?accept=yes&consentMessageIds[]=24")

# your frame is getting changed you need to switch the frame and wait for it

WebDriverWait(driver,30).until(EC.frame_to_be_available_and_switch_to_it('fillform-frame-1'))

WebDriverWait(driver,30).until(EC.element_to_be_clickable((By.XPATH,'//*[@id="PostcodeSearch"]')))
time.sleep(1)
# assert "Check bin collections" in driver.title
elem = driver.find_element_by_id('PostcodeSearch')
elem.clear()
elem.send_keys("RH10 7AB")
elem.send_keys(Keys.RETURN)
Hietsh Kumar
  • 1,197
  • 9
  • 17
  • Thanks. I'm now at the page with the hits but I think there's another iframe somewhere? I have `collection_dict = {} collectiondetails = {"nextrubbish" : (driver.find_element_by_name("rubbishDateNext").text)} print(collectiondetails["nextrubbish"])` and it returns nothing but I was hoping it would return Friday 26 June ? – Adam Davies Jun 25 '20 at 10:33
1

The postcode field 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 either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    driver.get('https://my.crawley.gov.uk/en/service/check_my_bin_collection?accept=yes&consentMessageIds[]=24')
    elem = WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[title='Check bin collections']")))
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#PostcodeSearch")))
    elem.send_keys("RH10 7AB")
    
  • Using XPATH:

    driver.get("https://my.crawley.gov.uk/en/service/check_my_bin_collection?accept=yes&consentMessageIds[]=24")
    elem = WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@title='Check bin collections']")))
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='PostcodeSearch']")))
    elem.send_keys("RH10 7AB")
    
  • 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
    
  • Browser Snapshot:

crawley


Reference

You can find a relevant discussion in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Thanks! I'm now able to get to the page with the results but I think there's another iframe in there I'm not spotting? – Adam Davies Jun 25 '20 at 10:32