I am scraping gas and electricity prices at uswitch.com and have got to the end of the process where the website outputs prices. I want to print the cost of the cheapest deal, after including plans that require switching directly through the supplier. The code below does this, after copying the xpath of the cheapest deal
//*[@id='my-results']/div[3]/div[2]/div[1]/div/div[2]/div/div[2]/a/div[1]/span[2]/span[1]
, but the result printed to screen is not the cheapest deal, but two frames down, the cheapest deal not including plans that requires switching directly through the supplier, which I enabled. For me this is just selenium picking the wrong div, but maybe something dynamic is causing the code problems.
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support import ui
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import TimeoutException
driver = webdriver.Chrome(executable_path=ChromeDriverManager().install())
# Open webpage
driver.get('https://www.uswitch.com/gas-electricity/')
# Locates 'input postcode' box
search_form = driver.find_element_by_id('input-postcode')
# eneters a postcode in the 'input postcode' box
search_form.send_keys('SW7 2BX')
# Click on the 'Compare energy deals now' to submit the form and continue
driver.find_element_by_xpath("//*[contains(text(),'Compare energy deals now')]").click()
# Once the page loads, wait for 2 seconds
driver.implicitly_wait(2)
# Click on the 'Skip' button to skip selecting an address
driver.find_element_by_css_selector('.us-link.us-margin-top.us-float-- right').click()
#Once the page loads, wait for 2 seconds THIS ISNT WORKING????? (NOR ARE THE OTHERS)
driver.implicitly_wait(2)
# Click on the 'Continue' button under 'Your property'
driver.find_element_by_xpath("//*[contains(text(),'Continue')]").click()
# Click on the 'Continue' button under 'Your supply'
driver.find_element_by_xpath("//*[contains(text(),'Continue')]").click()
# Click on the 'Continue' button under 'Your gas and electricity'
driver.find_element_by_xpath("//*[contains(text(),'Continue')]").click()
# Click on the 'Continue' button under 'Your usage'
driver.find_element_by_xpath("//*[contains(text(),'Continue')]").click()
# Under 'your gas usage', select the 'I use ...' option
driver.find_element_by_xpath("//input[@name='usage-gas' and @value='usage']").click()
# Creating a variable that represents the gas usage box
gas_usage_box=driver.find_element_by_xpath("//input[@name='usage-for-gas' and @class='us-form-input']")
# Locates the 'I use ...' box under 'Your gas usage'
gas_usage_box.click()
# Enters AQ into 'I use ...' box
gas_usage_box.send_keys('1001')
# Click on 'Continue' button under 'Your gas usage box'
driver.find_element_by_xpath("//*[contains(text(),'Continue')]").click()
# Under 'your electricity usage', select the 'I use ...' option
driver.find_element_by_xpath("//input[@name='usage-electricity' and @value='usage']").click()
# Creating a variable that represents the electricity usage box
electricity_usage_box=driver.find_element_by_xpath("//input[@name='usage- for-electricity' and @class='us-form-input']")
# Locates the 'I use ...' box under 'Your gas usage'
electricity_usage_box.click()
# Enters EAC into 'I use ...' box
electricity_usage_box.send_keys('1001')
# Wait 5 seconds after enetering EAC
wait = ui.WebDriverWait(driver, 5)
# Click 'Find cheaper deals' button under 'Your electricity usage'
driver.find_element_by_xpath("//*[contains(text(),'Find cheaper deals')]").click()
# Wait 5 seconds after enetering EAC
wait = ui.WebDriverWait(driver, 5)
# Choose to view plans that 'require switching directly through the supplier'
#driver.find_element_by_xpath("//input[@name='show-switchable-plans- filters' and @class='fulfilability-toggle__input us-form-input js- fulfilability-toggle' and @type='radio' and @value='false']")
driver.find_element_by_xpath("//input[@name='show-switchable-plans-filters' and @value='false']").click()
wait = ui.WebDriverWait(driver, 20)
mytext = driver.find_element_by_xpath("//*[@id='my- results']/div[3]/div[2]/div[1]/div/div[2]/div/div[2]/a/div[1]/span[2]/span[1]")
print mytext.get_attribute('innerHTML')
The only difference I can see between the two different types of prices is the one which requires switching directly through the supplier is inside a class of type 'unfufillable', which may aid an xpath search, which I am new to.
<div class="new-result-mobile-row__wrapper new-result-mobile-row__wrapper--
unfulfillable">
<span>
Estimated cost
</span><span class="new-result-mobile-row__wrapper__link__cost__figure">
<span class="new-result-mobile-row__wrapper__link__cost__figure__pounds">
£257</span>
<span class="new-result-mobile-row__wrapper__link__cost__figure__pence">
.19</span></span>
<span>per year</span>
<span>
(£21.43 pm)
EDIT:
SOLUTION: I don't know what the problem was but inserting driver.refresh() instead of the webdriver.wait, solved the problem.