使用 Python Selenium 与 React 图表交互

问题描述 投票:0回答:2

我正在尝试使用 Python Selenium 按下网页 https://coinmarketcap.com/currencies/vitex-coin/ 上的 React 图表中的按钮。我尝试使用 xpath,但我想要一种更通用的方法,因为它应该与同一网站的其他网页一起使用。另外,我想按下载按钮并选择 csv。特别是,我想先按“全部”按钮,然后下载 csv。这是我的尝试:

from selenium import webdriver

url='https://coinmarketcap.com/currencies/vitex-coin/'

driver = webdriver.Chrome()
driver.get(url)

button=driver.find_element('xpath', '/html/body/div[1]/div/div[1]/div[2]/div/div[3]/div/div[1]/div[2]/div[1]/div/div/div/div[2]/div/div/div[2]/div/div[2]/div/ul/li[5]')
button.click()

谢谢!

python reactjs selenium-webdriver
2个回答
1
投票

您可以使用下面的代码,应该可以工作 仅供参考,我添加了代码来最大化窗口,因为如果窗口未最大化,下载 csv 按钮将无法正确显示

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

url = 'https://coinmarketcap.com/currencies/vitex-coin/'

driver = webdriver.Chrome()
driver.get(url)
driver.maximize_window()
wait = WebDriverWait(driver, 10)
allButton = wait.until(
    EC.presence_of_element_located((By.XPATH, '(//ul[@class="react-tabs__tab-list"])[1]//li[text()="ALL"]')))
allButton.click()
exportButton = allButton.find_element(By.XPATH, "(//li//div[contains(@class,'custom-button-inner')])[2]")
actions = ActionChains(driver)
actions.move_to_element(exportButton).pause(1).click().perform()
downloadAsCsvButton = wait.until(
    EC.visibility_of_element_located((By.XPATH, '//button[text()="Download price history (.csv)"]')))
downloadAsCsvButton.click()
# Wait for download to complete
time.sleep(10)

0
投票

网站发生了一些变化,这是我的新尝试。

"Download price history (.csv)"
似乎大部分时间都不起作用。我也添加了一个
div
,但什么也没有。网页随机变化,但参考应该稳定。请参阅下面更新的代码,现在您必须处理弹出的cookie。

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

url = 'https://coinmarketcap.com/currencies/render/'

driver = webdriver.Chrome()
driver.get(url)
driver.maximize_window()
wait = WebDriverWait(driver, 10)

# interact with chart and press download button
for scroll in [0, 300, 600, 900, 1200]:
    try:
        driver.execute_script(f"window.scrollTo(0, {scroll});")
        allButton=WebDriverWait(driver, 3).until(
#             EC.presence_of_element_located((By.XPATH, '(//ul[@class="react-tabs__tab-list"])[1]//li[text()="ALL"]')))
            EC.presence_of_element_located((By.XPATH, '(//ul[@class="react-tabs__tab-list"])[1]//li[text()="All"] | (//ul[@class="react-tabs__tab-list"])[1]//li[text()="ALL"]')))

        allButton.click()
        break
    except:
        pass
    if check_cookies:
        for pp in cookie_list:
            try:              # close cookies
                cookie=WebDriverWait(driver, 2).until(
                    EC.presence_of_element_located((By.XPATH, pp)))
                cookie.click()
                break
            except:
                pass
exportButton=allButton.find_element(By.XPATH, "(//li//div[contains(@class,'custom-button-inner')])[2]")
actions=ActionChains(driver)
actions.move_to_element(exportButton).pause(2).click().perform()
downloadAsCsvButton=wait.until(
                    EC.visibility_of_element_located((By.XPATH, '//button//div[text()="Download price history (.csv)"]')))
downloadAsCsvButton.click()

© www.soinside.com 2019 - 2024. All rights reserved.