如何在Investing.com中正确输入日期?

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

我尝试使用日期变量类型和命令 .send_keys("inputDate") 输入日期范围,其中“inputDate”是像“2020-01-02”这样的字符串值,但即使代码正确运行,输入的日期值与我想要的日期完全不同。

我尝试使用“from datetime import datetime”或“import date”将变量类型从 Str 切换为 date,我希望它能解决问题,但事实并非如此。我还直接检查了网站中的输入字段,即以下行:

<input class="absolute left-0 top-0 h-full w-full opacity-0" type="date" max="2024-05-29" value="2024-04-28">

还有 XPath:

//*[@id="__next"]/div[2]/div[2]/div[2]/div[1]/div[3]/div[2]/div[2]/div[3]/div[1]/div[1]/input

并检查输入的顺序(日-月-年)是否正确。还是没成功。

没有错误代码,只是输入了错误的日期

这是我的代码:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
import time
from datetime import date

opt = webdriver.ChromeOptions()
opt.add_argument("--start-maximized")
opt.add_argument("--enable-javascript")

prefs = {"download.default_directory": "Downloads"}  # Change this to your desired directory
opt.add_experimental_option("prefs", prefs)
opt.add_experimental_option("excludeSwitches", ["disable-popup-blocking"])

userAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36'

opt.add_argument(f'user-agent={userAgent}')

driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=opt)  # 16
time.sleep(2)
driver.execute_cdp_cmd("Page.removeScriptToEvaluateOnNewDocument", {"identifier":"1"})

driver.get(r'https://www.investing.com/portfolio');
assert 'Portfolio & Watchlist - Investing.com' in driver.title
print('Opened site successfully')

username = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.ID, 'loginFormUser_email')))
username.send_keys("", 'my Email')
print('Entered UserID')
password = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.ID, 'loginForm_password')))
password.send_keys("", 'my Password')
print('Entered Password')
driver.find_element(By.XPATH,#Login
                     '//*[@id="signup"]/a').click()

links = ['https://www.investing.com/commodities/aluminum-historical-data',
         'https://www.investing.com/commodities/copper-historical-data?cid=959211',
         'https://www.investing.com/commodities/silver-historical-data',
         'https://www.investing.com/equities/steel-dynamics-historical-data',
         'https://www.investing.com/commodities/iron-ore-62-cfr-futures-historical-data'
         ]

for link in links:
    print(link)
    driver.get(link)
    driver.find_element(By.XPATH,
                         '//*[@id="__next"]/div[2]/div[2]/div[2]/div[1]/div[2]/div[2]/div[2]/div[1]').click()
    driver.find_element(By.XPATH,
                         '//*[@id="__next"]/div[2]/div[2]/div[2]/div[1]/div[2]/div[2]/div[2]/div[2]/div[1]/div[1]/input').send_keys('02-05-2022')
    driver.find_element(By.XPATH,
                         '//*[@id="__next"]/div[2]/div[2]/div[2]/div[1]/div[2]/div[2]/div[2]/div[2]/div[2]').click()
    driver.find_element(By.XPATH, '//*[@id="__next"]/div[2]/div[2]/div[2]/div[1]/div[2]/div[2]/div[2]/a/div').click()
python datetime selenium-webdriver
1个回答
0
投票

你可以尝试通过JS来输入

ele = .driver.find_element_by_xpath(your_xpath)
driver.execute_script("arguments[0].value=arguments[1]", ele, '2020-01-02')
© www.soinside.com 2019 - 2024. All rights reserved.