DAVIDBALAS1
DAVIDBALAS1

Reputation: 484

Python: selenium select

I have a website with some different prices currencies you can select in a <select> with <option>, it looks like this:

<div class="menu" style="display: none;">
        <p class="current-country">
            <span class="flag store-7"></span>
            Italia
        </p>
        <div class="currency-list">
            <label for="currencyList">Cambia valuta:</label>
            <select name="currency-list" id="currencyList" data-bind="valueFromOptions: currencies, value: selectedCurrencyId">
                        <option value="1" data-label="GBP">£ GBP</option>
                        <option value="2" data-label="USD">$ USD</option>
                        <option value="3" data-label="CAD">C$ CAD</option>
                        <option value="8" data-label="SEK">kr SEK</option>
                        <option value="9" data-label="NOK">kr NOK</option>
                        <option value="10" data-label="DKK">kr DKK</option>
                        <option value="14" data-label="CHF">₣ CHF</option>
                        <option value="19" data-label="EUR"> € EUR</option>
                        <option value="21" data-label="AUD">$ AUD</option>
                        <option value="10021" data-label="RMB">¥ RMB</option>
                        <option value="10042" data-label="HKD">$ HKD</option>
                        <option value="10064" data-label="NZD">$ NZD</option>
                        <option value="10078" data-label="SGD">$ SGD</option>
                        <option value="10085" data-label="TWD">NT$ TWD</option>
                        <option value="10123" data-label="RUB">руб.  RUB</option>
            </select>
        </div>
    </div>

I want to iterate between the options available and each time print the price of the item, however, in this website, everytime you change the currency it reloads the page, so the code is not working for me, here is my code:

driver.get(root_url[i] + str(num))  # open the page
el = driver.find_element_by_name('currency-list')  # find the <select>
for option in el.find_elements_by_tag_name('option'):  # for options inside
        div = driver.find_element_by_class_name('menu')  
        driver.execute_script("document.getElementsByClassName('menu')[0].style.display = 'block';")  # make the div containing the <select> visible
        option.click()
        elem = driver.find_element_by_class_name('current-price')  # find the price element and print it
        print(elem.get_attribute('innerHTML'), root_url[i] + str(num), option.get_attribute('innerHTML'))
driver.close()

I get an error:

Traceback (most recent call last):
  File "C:/Users/dodob/PycharmProjects/Learning/Asosly.py", line 17, in <module>
    print(elem.get_attribute('innerHTML'), root_url[i] + str(num), option.get_attribute('innerHTML'))
  File "C:\Users\dodob\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 111, in get_attribute
    resp = self._execute(Command.GET_ELEMENT_ATTRIBUTE, {'name': name})
  File "C:\Users\dodob\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 461, in _execute
    return self._parent.execute(command, params)
  File "C:\Users\dodob\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 236, in execute
    self.error_handler.check_response(response)
  File "C:\Users\dodob\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 192, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.StaleElementReferenceException: Message: Element not found in the cache - perhaps the page has changed since it was looked up
Stacktrace:
    at fxdriver.cache.getElementAt (resource://fxdriver/modules/web-element-cache.js:9454)
    at Utils.getElementAt (file:///C:/Users/dodob/AppData/Local/Temp/tmpb9cwfwq1/extensions/[email protected]/components/command-processor.js:9039)
    at WebElement.getElementAttribute (file:///C:/Users/dodob/AppData/Local/Temp/tmpb9cwfwq1/extensions/[email protected]/components/command-processor.js:12146)
    at DelayedCommand.prototype.executeInternal_/h (file:///C:/Users/dodob/AppData/Local/Temp/tmpb9cwfwq1/extensions/[email protected]/components/command-processor.js:12661)
    at DelayedCommand.prototype.executeInternal_ (file:///C:/Users/dodob/AppData/Local/Temp/tmpb9cwfwq1/extensions/[email protected]/components/command-processor.js:12666)
    at DelayedCommand.prototype.execute/< (file:///C:/Users/dodob/AppData/Local/Temp/tmpb9cwfwq1/extensions/[email protected]/components/command-processor.js:12608)

I am sorry for my English, but anyway, what can I do to wait till the page reloads because I think it makes the issue.

If needed, I am trying to scrape some info from ASOS, example link here

Upvotes: 0

Views: 285

Answers (1)

thebadguy
thebadguy

Reputation: 2140

# -*- coding: utf-8 -*-

from selenium import webdriver
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException


driver = webdriver.Chrome(\Path to chrome driver\)
driver.maximize_window()
baseurl = "http://www.asos.com/it/asos/asos-jeans-skinny-alla-caviglia-kaki/prd/6759361"
driver.get(baseurl)



def Getprice():
    selected_currency = driver.find_element(By.CSS_SELECTOR,".selected-currency")
    currentprice = driver.find_element(By.CSS_SELECTOR,".current-price")
    print "The price for selected current currency " + selected_currency.text + " is " +currentprice.text

def Setcurrency(text):
    one = driver.find_element(By.CLASS_NAME, "selected-currency")
    one.click()
    currentselection = driver.find_element_by_id("currencyList")
    select = Select(currentselection)
    select.select_by_visible_text(text)

def getallcurrencies():

    one = driver.find_element(By.CLASS_NAME, "selected-currency")
    one.click()
    el = driver.find_element_by_id("currencyList")
    currency =[]
    for option in el.find_elements_by_tag_name('option'):
        currency.append((option.text).encode('utf8'))
    return currency

list_of_currencies = getallcurrencies()
for currency in list_of_currencies:
    currentvalue= currency.decode('utf8')
    try:
        Setcurrency(currentvalue)
        time.sleep(2)
        element_present = EC.presence_of_element_located((By.CSS_SELECTOR,".current-price"))
        WebDriverWait(driver, 10).until(element_present)
        Getprice()
    except TimeoutException:
        print "Timed out waiting for page to load"

This program will first get all the avaiable currencies & then print all the prices :

The price for selected current currency £ GBP is £ 25,49
The price for selected current currency $ USD is $ 41,10
The price for selected current currency C$ CAD is C$ 50,85
The price for selected current currency kr SEK is SEK 318,66
The price for selected current currency kr NOK is NOK 326,83
The price for selected current currency kr DKK is DKK 254,93
The price for selected current currency ₣ CHF is CHF 38,95
The price for selected current currency € EUR is € 33,99
The price for selected current currency $ AUD is $ 54,94
The price for selected current currency ¥ RMB is ¥ 245,12
The price for selected current currency $ HKD is HKD$ 310,89
The price for selected current currency $ NZD is NZD$ 62,13
The price for selected current currency $ SGD is SGD$ 55,41
The price for selected current currency NT$ TWD is NT$ 1.274,65
The price for selected current currency руб. RUB is руб. 2.338,81

Hope this will help in resolving your problem

Upvotes: 1

Related Questions