无法从网页获取特定金额的正确值

时间:2018-02-11 14:21:10

标签: python python-3.x selenium selenium-webdriver web-scraping

我使用selenium在python中编写了一个脚本来获取一定数量的转换值。当前一个放入占位符时,该金额产生转换值。新生成的值与金额相邻。当我在该占位符中手动输入任何金额时,我会相应地获得转换后的值,但是当我以编程方式执行相同操作时,该值保持不变,因此我的刮刀将0作为值。我怎样才能使它发挥作用?

指向该网页的链接:weblink

我尝试过的脚本:

from selenium.webdriver import Chrome
from contextlib import closing
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys

with closing(Chrome()) as driver:
    wait = WebDriverWait(driver, 10)
    driver.get("find_the_link_above")
    wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, ".OrderForm_input-box_XkGmi input[name='amount']"))).send_keys(100)
    item = wait.until(EC.presence_of_element_located((By.CLASS_NAME,"OrderForm_total_6EL8d"))).text
    print(item)

当我手动向占位符添加任何金额时,可以看到如下所示的更改:

enter image description here

但是,当我使用脚本执行相同操作时,它的外观如下:

enter image description here

我用黑色标记了值,让你知道我的意思。

1 个答案:

答案 0 :(得分:1)

问题是您过早发送该值,以便在输入金额值后不反映该值。在这里,我等待EUR SPREAD元素在设置金额值之前加载。您可以使用相同的元素或您选择的任何其他元素,但确保页面完全加载该对象,然后发送金额值。

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
chrome_path = r"path"
driver = webdriver.Chrome(chrome_path)
wait = WebDriverWait(driver, 10)
driver.get("https://www.gdax.com/trade/LTC-EUR")
wait.until(EC.presence_of_element_located((By.XPATH, "//div[@class='OrderBookPanel_text_33dbp']")))
wait.until(EC.element_to_be_clickable((By.XPATH, "//input[@name='amount']"))).send_keys(100)
item = wait.until(EC.presence_of_element_located((By.CLASS_NAME,"OrderForm_total_6EL8d"))).text
print(item)

希望这能解决您的问题。