硒 - 在网页中选择日期时问题

发布于 2025-01-22 06:20:12 字数 2824 浏览 0 评论 0原文

我正在尝试从网页中选择一些数据: https://indiawris.gov.in/wris/ #/地下水 在选择网页中的开始日期时,我面临问题。我已经尝试使用与此类似的代码相似的尝试,但是在这里它不起作用。

#code snippet tried for date picking earlier which is not working now
    startdate_ele = driver.find_element_by_id('start_date_1')
    startdate_ele.clear()
    startdate = '2016/11/01' ## set the start date
    end_date_ele = driver.find_element_by_id('end_date_1')
    end_date_ele.clear()
    endDate ='2017/07/31'   ## set the end date for files to download
    startdate_ele.send_keys(startdate)
    time.sleep(5)
    end_date_ele.send_keys(endDate)

以下是当前代码。

#functional code that does selection prior to the date picking

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

path2 = r"F:\\chromedriver_win32\\chromedriver.exe"
driver = webdriver.Chrome(executable_path=path2)
driver.maximize_window()

wait = WebDriverWait(driver, 30)

driver.get("https://indiawris.gov.in/wris/#/groundWater")

# try:
wait.until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//iframe[@class='ng-star-inserted']")))
print('Switched successfully to iframe')
ele = wait.until(EC.element_to_be_clickable((By.XPATH, "//div[@class='views']//label[contains(.,'Basin')]//input")))
driver.execute_script("arguments[0].click();", ele)
print('Clicked on basin button')
sourceDrop = driver.find_element_by_xpath('//*[@id="applications"]/div[2]/div/groundwater-sidebar/div/div[2]')
sourceItems = sourceDrop.find_elements_by_class_name('ng-star-inserted')
print("length",len(sourceItems))
actionChains = ActionChains(driver)
select = Select(driver.find_element_by_xpath('//*[@id="applications"]/div[2]/div/groundwater-sidebar/div/div[2]/select'))
select.select_by_visible_text('CGWB')

select = Select(driver.find_element_by_xpath('//*[@id="applications"]/div[2]/div/groundwater-sidebar/div/div[3]/select'))
select.select_by_visible_text('yyyyMMdd')

startdate_ele = driver.find_element_by_xpath('//*[@id="applications"]/div[2]/div/groundwater-sidebar/div/div[4]/datepicker/div/input')
startdate_ele.clear()   
startdate = '03-January-2021' ## set the start date
end_date_ele = driver.find_element_by_id('end_date_1')
end_date_ele.clear()
endDate ='2017/07/31'   ## set the end date for files to download
startdate_ele.send_keys(startdate)
time.sleep(5)
end_date_ele.send_keys(endDate)

有人可以帮助我从该门户网站挑选日期进行选择。

I am trying to select some data from webpage: https://indiawris.gov.in/wris/#/groundWater
I am facing problem while picking the start and end dates in the webpage. I have made an attempt using a code similar to this earlier, but here it did not work.

#code snippet tried for date picking earlier which is not working now
    startdate_ele = driver.find_element_by_id('start_date_1')
    startdate_ele.clear()
    startdate = '2016/11/01' ## set the start date
    end_date_ele = driver.find_element_by_id('end_date_1')
    end_date_ele.clear()
    endDate ='2017/07/31'   ## set the end date for files to download
    startdate_ele.send_keys(startdate)
    time.sleep(5)
    end_date_ele.send_keys(endDate)

Following is the current code.

#functional code that does selection prior to the date picking

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

path2 = r"F:\\chromedriver_win32\\chromedriver.exe"
driver = webdriver.Chrome(executable_path=path2)
driver.maximize_window()

wait = WebDriverWait(driver, 30)

driver.get("https://indiawris.gov.in/wris/#/groundWater")

# try:
wait.until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//iframe[@class='ng-star-inserted']")))
print('Switched successfully to iframe')
ele = wait.until(EC.element_to_be_clickable((By.XPATH, "//div[@class='views']//label[contains(.,'Basin')]//input")))
driver.execute_script("arguments[0].click();", ele)
print('Clicked on basin button')
sourceDrop = driver.find_element_by_xpath('//*[@id="applications"]/div[2]/div/groundwater-sidebar/div/div[2]')
sourceItems = sourceDrop.find_elements_by_class_name('ng-star-inserted')
print("length",len(sourceItems))
actionChains = ActionChains(driver)
select = Select(driver.find_element_by_xpath('//*[@id="applications"]/div[2]/div/groundwater-sidebar/div/div[2]/select'))
select.select_by_visible_text('CGWB')

select = Select(driver.find_element_by_xpath('//*[@id="applications"]/div[2]/div/groundwater-sidebar/div/div[3]/select'))
select.select_by_visible_text('yyyyMMdd')

startdate_ele = driver.find_element_by_xpath('//*[@id="applications"]/div[2]/div/groundwater-sidebar/div/div[4]/datepicker/div/input')
startdate_ele.clear()   
startdate = '03-January-2021' ## set the start date
end_date_ele = driver.find_element_by_id('end_date_1')
end_date_ele.clear()
endDate ='2017/07/31'   ## set the end date for files to download
startdate_ele.send_keys(startdate)
time.sleep(5)
end_date_ele.send_keys(endDate)

can some one help me in picking the dates from this portal to do the selection.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谁的年少不轻狂 2025-01-29 06:20:12

您的代码当前有几个问题。

1. Use By.XPATH, By,CLASS_NAME, By.ID all of your items are depreciated.
2. select = Select(driver.find_element(By.XPATH,'//*[@id="applications"]/div[2]/div/groundwater-sidebar/div/div[3]/select'))
select.select_by_value('yyyyMMdd')

Using the wrong selection for the timestep

3. Target the input tag and send keys for start and stop they have something like

<input _ngcontent-qlb-c14="" bsdatepicker="" class="form-control ng-pristine ng-valid ng-star-inserted ng-touched" readonly="" ng-reflect-klass="form-control" ng-reflect-ng-class="[object Object]" ng-reflect-bs-config="[object Object]" ng-reflect-is-disabled="true" ng-reflect-max-date="Sat Jan 09 2021 00:00:00 GMT-0" ng-reflect-name="to" placeholder="Select to date" style="">

There are several issues currently wrong with your code.

1. Use By.XPATH, By,CLASS_NAME, By.ID all of your items are depreciated.
2. select = Select(driver.find_element(By.XPATH,'//*[@id="applications"]/div[2]/div/groundwater-sidebar/div/div[3]/select'))
select.select_by_value('yyyyMMdd')

Using the wrong selection for the timestep

3. Target the input tag and send keys for start and stop they have something like

<input _ngcontent-qlb-c14="" bsdatepicker="" class="form-control ng-pristine ng-valid ng-star-inserted ng-touched" readonly="" ng-reflect-klass="form-control" ng-reflect-ng-class="[object Object]" ng-reflect-bs-config="[object Object]" ng-reflect-is-disabled="true" ng-reflect-max-date="Sat Jan 09 2021 00:00:00 GMT-0" ng-reflect-name="to" placeholder="Select to date" style="">
太阳男子 2025-01-29 06:20:12

对于以下几个月,代码将有效地导航WRT年,我们需要添加更多逻辑。

eleq = wait.until(EC.element_to_be_clickable((By.XPATH,"(//span[@class='input-group-addon pointer'])[1]")))
driver.execute_script("arguments[0].click();", eleq)

driver.find_element_by_xpath("//span[contains(text(),'January')]").click()

#eleq = wait.until(EC.element_to_be_clickable((By.XPATH,"//span[@class='input-group-addon pointer']")))
eleqy = wait.until(EC.element_to_be_clickable((By.XPATH,"(//span[@class='input-group-addon pointer'])[2]")))
driver.execute_script("arguments[0].click();", eleqy)

driver.find_element_by_xpath("//span[contains(text(),'January')]").click()

For selecting Months below code will work to navigate w.r.t year we need to add more logic.

eleq = wait.until(EC.element_to_be_clickable((By.XPATH,"(//span[@class='input-group-addon pointer'])[1]")))
driver.execute_script("arguments[0].click();", eleq)

driver.find_element_by_xpath("//span[contains(text(),'January')]").click()

#eleq = wait.until(EC.element_to_be_clickable((By.XPATH,"//span[@class='input-group-addon pointer']")))
eleqy = wait.until(EC.element_to_be_clickable((By.XPATH,"(//span[@class='input-group-addon pointer'])[2]")))
driver.execute_script("arguments[0].click();", eleqy)

driver.find_element_by_xpath("//span[contains(text(),'January')]").click()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文