Reputation: 11
I am trying to input a date range using a date variable type and the command .send_keys("inputDate") where "inputDate" is a string value like "2020-01-02", but even when the code run correctly, the date value inputted is totally different from the date I wanted.
I have tried to switch variable types from Str to date using "from datetime import datetime" or "import date" and I was hoping it would fix the issue, but it didn't. I have also inspected directly the input field in the website, which is the following line:
<input class="absolute left-0 top-0 h-full w-full opacity-0" type="date" max="2024-05-29" value="2024-04-28">
and also it's XPath:
//*[@id="__next"]/div[2]/div[2]/div[2]/div[1]/div[3]/div[2]/div[2]/div[3]/div[1]/div[1]/input
and checked if the order of input (day-month-year) was correct. Still didn't work.
There is no error code, it just inputs the wrong date
Here's my code:
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()
Upvotes: 0
Views: 87
Reputation: 21
You can try to go through JS to input
ele = .driver.find_element_by_xpath(your_xpath)
driver.execute_script("arguments[0].value=arguments[1]", ele, '2020-01-02')
Upvotes: 0