我有一个网站,有一些不同价格的货币,您可以在<select>和<option>中选择,它看起来如下:
<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>我想在可用的选项之间进行迭代,每次打印项目的价格,但是,在这个网站中,每次您更改货币时,它都会重新加载页面,所以代码对我无效,下面是我的代码:
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()我收到一个错误:
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/fxdriver@googlecode.com/components/command-processor.js:9039)
at WebElement.getElementAttribute (file:///C:/Users/dodob/AppData/Local/Temp/tmpb9cwfwq1/extensions/fxdriver@googlecode.com/components/command-processor.js:12146)
at DelayedCommand.prototype.executeInternal_/h (file:///C:/Users/dodob/AppData/Local/Temp/tmpb9cwfwq1/extensions/fxdriver@googlecode.com/components/command-processor.js:12661)
at DelayedCommand.prototype.executeInternal_ (file:///C:/Users/dodob/AppData/Local/Temp/tmpb9cwfwq1/extensions/fxdriver@googlecode.com/components/command-processor.js:12666)
at DelayedCommand.prototype.execute/< (file:///C:/Users/dodob/AppData/Local/Temp/tmpb9cwfwq1/extensions/fxdriver@googlecode.com/components/command-processor.js:12608)我很抱歉我的英语,但无论如何,我能做什么,直到页面重新加载,因为我认为这是一个问题。
如果需要的话,我正在尝试从ASOS抓取一些信息,例如链接这里。
发布于 2016-10-10 03:32:00
# -*- 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"这个程序将首先获取所有可兑换货币,然后打印所有价格:
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希望这将有助于解决你的问题。
https://stackoverflow.com/questions/39934896
复制相似问题