与Python Selenium交互React图表

huangapple go评论81阅读模式
英文:

Interact with React chart with Python Selenium

问题

I'm trying to press a button in a React chart on the webpage https://coinmarketcap.com/currencies/vitex-coin/ by using Python Selenium. I tried with xpath but I'd like a more general way as it should work with other webpages from the same website. Also, I'd like to press the download button and select the csv. In particular, I want to press the 'ALL' button first and then download the csv. Here's my attempt:

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()

Thank you!
与Python Selenium交互React图表

英文:

I'm trying to press a button in a React chart on the webpage https://coinmarketcap.com/currencies/vitex-coin/ by using Python Selenium. I tried with xpath but I'd like a more general way as it should work with other webpages from the same website. Also, I'd like to press the download button and select the csv. In particular, I want to press the 'ALL' button first and then download the csv. Here's my attempt:

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()

Thank you!
与Python Selenium交互React图表

答案1

得分: 1

以下是您提供的代码的中文翻译:

您可以使用以下代码应该可以工作
FYI我已经添加了代码以最大化窗口因为如果窗口没有最大化下载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()
# 等待下载完成
time.sleep(10)

希望这对您有所帮助。如果您有任何其他问题,请随时提出。

英文:

You can use below code, should work
FYI I have added code to maximise windows as Download csv button doesnt show properly if window is not maximised

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)

huangapple
  • 本文由 发表于 2023年5月17日 23:21:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76273700.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定