Селен забирает неправильный div - PullRequest
0 голосов
/ 03 апреля 2019

Я снимаю цены на газ и электроэнергию на 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, решил проблему.

Ответы [ 2 ]

0 голосов
/ 03 апреля 2019

Используемые фиксированные локаторы, пожалуйста, ознакомьтесь с рекомендациями или Официальные стратегии локаторов для веб-драйвера .
Не используйте явное и неявное ожидание одновременно, отметьте , когда использовать явное ожиданиеи неявное ожидание .

Код является базовым примером, вы можете изменять, создавать методы .., чтобы сделать его более читабельным:

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)
0 голосов
/ 03 апреля 2019

Используйте 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"]')))
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...