简体   繁体   English

使用 Selenium 和 Python 从动态网站获取元素值

[英]Get element value from dynamic website using Selenium and Python

I'm trying to get the value of the vending price of AMZN index market directly from the trading platform plus500, the value changes continuosly so I have to use selenium.我正在尝试直接从交易平台plus500获取AMZN指数市场的卖价价值,价值不断变化,所以我必须使用selenium。 The code I'm using is this one:我正在使用的代码是这个:

driver.get("https://app.plus500.com/trade/amazon")
# get AMZN vending price
Sell = driver.find_elements(By.CLASS_NAME, value="sell")
print(Sell)

The html from the source is this:来源的 html 是这样的:

<div class="sell" data-no-trading="false" id="_win_plus500_bind873" data-show="true">126.28</div>

I need to scrape the value (in this case 126,28) every time it changes.每次更改时,我都需要抓取该值(在本例中为 126,28)。 If it is needed I created a dummy Plus500 account for you: username "myrandomcode@gmail.com" password: "MyRandomCode87".如果需要,我为您创建了一个虚拟 Plus500 帐户:用户名“myrandomcode@gmail.com”密码:“MyRandomCode87”。

To extract the value of the vending price of AMZN index market directly from the trading platform plus500 ie the text 126.28 as the element is a dynamic element you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following locator strategies :要直接从交易平台plus500中提取 AMZN 指数市场的销售价格值,即文本126.28作为元素是动态元素,您需要为visibility_of_element_located()诱导WebDriverWait ,您可以使用以下任一定位器策略

  • Using XPATH :使用XPATH

     print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[starts-with(@class, 'section-table-body')]//span[text()='Amazon']//following::div[2]"))).text)
  • Note : You have to add the following imports:注意:您必须添加以下导入:

     from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC

You can find a relevant discussion in How to retrieve the text of a WebElement using Selenium - Python您可以在如何使用 Selenium - Python 检索 WebElement 的文本中找到相关讨论

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM