我正在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')
我能看到的两种不同类型的价格之间的唯一区别是,需要直接通过供应商进行切换的价格类型位于“ unfufillable”类型内,这可能有助于xpath搜索,这是我的新手。 / p>
<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可以解决问题。
答案 0 :(得分:0)
使用WebDriverWait处理动态元素。
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
print(WebDriverWait(driver, 10).until(expected_conditions.element_to_be_clickable((By.XPATH, '//span[@class="new-result-mobile-row__wrapper__link__cost__figure__pounds"]'))).text)
已编辑。
print(driver.execute_script("return arguments[0].innerHTML;",driver.find_element_by_xpath('//span[@class="new-result-mobile-row__wrapper__link__cost__figure__pounds"]')))
答案 1 :(得分:0)
您使用的固定定位器,请检查最佳做法或official locator strategies for the webdriver。
不要同时使用显式和隐式等待,请检查when to use explicit wait vs. implicit wait。
代码是一个基本示例,您可以修改,创建方法..以使其更具可读性:
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.Chrome()
wait = WebDriverWait(driver, 15)
# Open webpage
driver.get('https://www.uswitch.com/gas-electricity/')
driver.find_element_by_id('input-postcode').send_keys('SW7 2BX')
# Click on the 'Compare energy deals now' to submit the form and continue
driver.find_element_by_xpath("//button[.='Compare energy deals now']").click()
# Click on the 'Skip' button to skip selecting an address
wait.until(EC.element_to_be_clickable((By.LINK_TEXT, "Skip"))).click()
# Click on the 'Continue' button under 'Your property'
driver.find_element_by_css_selector("#your-property .continue-button button").click()
# Click on the 'Continue' button under 'Your supply'
driver.find_element_by_css_selector("#your-supply .continue-button button").click()
# Click on the 'Continue' button under 'Your gas and electricity'
driver.find_element_by_css_selector("#dual-fuel .continue-button button").click()
# Click on the 'Continue' button under 'Your usage'
driver.find_element_by_css_selector("#your-usage .continue-button button").click()
# Under 'your gas usage', select the 'I use ...' option
driver.find_element_by_css_selector("input[name=usage-gas][value=usage]").click()
# Creating a variable that represents the gas usage box
gas_usage_input = driver.find_element_by_css_selector("input[name=usage-for-gas]")
# Locates the 'I use ...' box under 'Your gas usage'
gas_usage_input.click()
# Enters AQ into 'I use ...' box
gas_usage_input.send_keys('1001')
# Click on 'Continue' button under 'Your gas usage box'
driver.find_element_by_css_selector("#gas-usage .continue-button button").click()
# Under 'your electricity usage', select the 'I use ...' option
driver.find_element_by_css_selector("input[name=usage-electricity][value=usage]").click()
# Creating a variable that represents the electricity usage box
electricity_usage_input = driver.find_element_by_css_selector("input[name=usage-for-electricity]")
# Locates the 'I use ...' box under 'Your gas usage'
electricity_usage_input.click()
# Enters EAC into 'I use ...' box
electricity_usage_input.send_keys('1001')
# Click 'Find cheaper deals' button under 'Your electricity usage'
driver.find_element_by_css_selector(".cheaper-deals button").click()
# Choose to view plans that 'require switching directly through the supplier'
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.fulfilability-toggle__input[value=false]"))).click()
lowest_price = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "span.new-result-row__cost__figure"))).text
lowest_price_pound = driver.find_element_by_css_selector("span.new-result-row__cost__figure__pounds").text
lowest_price_pence = driver.find_element_by_css_selector("span.new-result-row__cost__figure__pence").text.replace(".", "")
print(lowest_price)