skip to Main Content

This is a work-in-progress project where I enter a postcode (which will eventually become a list) into a website and then save the results from the website to a CSV using some kind of loop to move onto the next.

I’m having issues extracting the results from below

enter image description here

**Great news! You can connect to the CityFibre network**

Below is the code I’ve created so far, and I would love any pointers or improvements.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
import time

driver = webdriver.Firefox()

driver.get('https://cityfibre.com/')
assert 'CityFibre' in driver.title

time.sleep(2.5)

cookies = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "onetrust-accept-btn-handler")))
cookies.click()


PostCode = driver.find_element(By.XPATH,("//input[@class='w-full border rounded-lg border-grey-950 bg-white text-grey-950 ac-outline focus:border-cf-blue placeholder-grey-500 md:text-base lg:rounded-xl lg:rounded-tl-none lg:rounded-tl-none rounded-tl-none pr-32 pl-11 py-5']"));
PostCode.click()
PostCode.send_keys('EH17 7RY')
PostCode.send_keys(Keys.ENTER)
time.sleep(2.5)
PostCode.send_keys(Keys.TAB)
PostCode.send_keys(Keys.ENTER)

time.sleep(15)

message = driver.find_element(By.TAG_NAME,('h2')[0]).text
print(message) 
print('The End')

2

Answers


  1. Chosen as BEST ANSWER

    Here is the refined code that also uses WebDriverWait

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.common.keys import Keys
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.support.wait import WebDriverWait
    import time
    
    driver = webdriver.Firefox()
    
    driver.get('https://cityfibre.com/')
    assert 'CityFibre' in driver.title
    
    cookies = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "onetrust-accept-btn-handler")))
    cookies.click()
    
    PostCode = driver.find_element(By.XPATH,("//input[@class='w-full border rounded-lg border-grey-950 bg-white text-grey-950 ac-outline focus:border-cf-blue placeholder-grey-500 md:text-base lg:rounded-xl lg:rounded-tl-none lg:rounded-tl-none rounded-tl-none pr-32 pl-11 py-5']"));
    PostCode.click()
    #PostCode.send_keys('EH17 7RY')     #multi house
    PostCode.send_keys('AB15 8AB')      #single house
    PostCode.send_keys(Keys.ENTER)
    
    
    House = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH,("//div[contains(@class, 'absolute inset-0 overflow-x-hidden overflow-y-scroll')]"))))
    House = driver.find_element(By.XPATH,("//input[@class='w-full border rounded-lg border-grey-950 bg-white text-grey-950 ac-outline focus:border-cf-blue placeholder-grey-500 md:text-base lg:rounded-xl lg:rounded-tl-none lg:rounded-tl-none rounded-tl-none pr-32 pl-11 py-5']"));
    House.click()
    House.send_keys(Keys.TAB)
    House.send_keys(Keys.ENTER)
    
    time.sleep(10)
    
    #message = driver.find_element(By.XPATH,("//h2[contains(text(),'*')]")).text
    
    message = driver.find_element(By.CSS_SELECTOR, '#js-availability-status-header h2').text
    print(message) 
    print('The End')
    
    """
    #driver.quit()
    """
    

  2. You have a typo in your code.

    You are getting first element from selector h2 that is actually illegal action.

    message = driver.find_element(By.TAG_NAME,('h2')[0]).text
    

    I think, you meant

    message = driver.find_elements(By.TAG_NAME, 'h2')[0].text
    

    Also, I suggest you to make locator more concrete and unique:

    message = driver.find_element(By.CSS_SELECTOR, '#js-availability-status-header h2').text
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search