英文:
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()
英文:
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()
答案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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论