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