Я снимаю цены на газ и электроэнергию на uswitch.com и дошел до конца процесса, когда веб-сайт выводит цены.Я хочу напечатать стоимость самой дешевой сделки, после того как включу планы, которые требуют переключения напрямую через поставщика.Приведенный ниже код делает это после копирования xpath самой дешевой сделки
//*[@id='my-results']/div[3]/div[2]/div[1]/div/div[2]/div/div[2]/a/div[1]/span[2]/span[1]
, но результат, выводимый на экран, - не самая дешевая сделка, а на два кадра ниже, самая дешевая сделка, не включая планы, требующие переключениянапрямую через поставщика, которого я включил.Для меня это просто селен, выбирающий неправильный div, но, возможно, что-то динамическое вызывает проблемы с кодом.
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')
Единственное различие, которое я вижу между двумя разными типами цен, это то, что требует прямого переключениячерез поставщика находится внутри класса типа «незаполняемый», который может помочь в поиске xpath, который я новичок.
<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)
РЕДАКТИРОВАТЬ:
РЕШЕНИЕ: я не знаюв чем проблема, но вставив driver.refresh () вместо webdriver.wait, решил проблему.