![](/img/trans.png)
[英]Selenium: How can i compare values from same type class name and click the link with highest value using python
[英]How do I get a value from a class name using selenium in python?
我正在嘗試從 class 名稱中獲取一個值,但現在唯一可以得到的是 [] output。 那么,我應該在下面的代碼中做什么?
from selenium import webdriver
import time
options = webdriver.ChromeOptions()
options.add_argument('lang=pt-br')
driver = webdriver.Chrome(
executable_path=r'./chromedriver.exe', options=options)
driver.get('https://economia.uol.com.br/cotacoes/cambio/')
time.sleep(5)
dolar = driver.find_elements_by_class_name('currency2')
time.sleep(5)
print(dolar)
您可以使用get_attribute檢索 web 元素的值,此外,在您的代碼中,您沒有 class 名稱為 currency2 的元素。
請找到以下示例:
<input class="field normal" name="currency2" value="5,59" data-audience-click="{"reference":"ativar-campo-texto","component":"currency-converter"}" xpath="1">
檢索值的代碼:
driver.get('https://economia.uol.com.br/cotacoes/cambio/')
currency = driver.find_element_by_name('currency2')
print currency.get_attribute("value")
Output::
5,59
您不需要selenium
來獲取該信息,請嘗試:
import requests
u = "https://api.cotacoes.uol.com/currency/intraday/list/?format=JSON&fields=bidvalue,askvalue,maxbid,minbid,variationbid,variationpercentbid,openbidvalue,date¤cy=1"
j = requests.get(u).json()
dolar = j['docs'][0]['bidvalue']
# 5.5916
筆記:
variationpercentbid
) 搜索 json object:currency=5
,將為您提供EUR
值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.