【问题标题】:Can't choose the correct end date from the datepicker in a Python script无法从 Python 脚本中的日期选择器中选择正确的结束日期
【发布时间】:2022-02-23 02:02:04
【问题描述】:

我使用的是谷歌 Chrome 浏览器。我正在运行一个 python 脚本来在日期选择器中选择正确的日期。无法选择正确的日期。它一直选择结束日期为“02/01/2022”,但我想在每次运行脚本时从今天的日期选择五 (5) 天前的日期。例如,今天是“02/08/2022”,因此应该选择“02/03/2022”作为结束日期。开始日期“2021 年 12 月 1 日”是正确的。

这是我的代码:

from selenium import webdriver
import time
import os.path
from selenium.webdriver.chrome.options import Options
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.common.exceptions import TimeoutException
import calendar
import datetime
from datetime import date

chrome_options = Options()
chrome_options.add_argument("--incognito")
driver = webdriver.Chrome("/Users/myname/Documents/chromedriver", options=chrome_options)

todays_date = date.today()
print(todays_date)

driver.get("https://accessdata.broadridge.com/node952064/")
try:
    myElem = WebDriverWait(driver, 3).until(EC.presence_of_element_located((By.XPATH, '//*[@id="main"]/table/tbody/tr[2]/td[2]/form/table[2]/tbody/tr[3]/td[2]/input')))
    print("Page is ready!")
except TimeoutException:
    print("Loading took too much time!")

driver.find_element(By.XPATH, '//*[@id="main"]/table/tbody/tr[2]/td[2]/form/table[2]/tbody/tr[6]/td[2]/span/img').click()

driver.find_element(By.XPATH, '//*[@id="span8"]').click()
time.sleep(2)
driver.find_element(By.XPATH, '//*[@id="ext-gen157"]/div[3]/table/tbody/tr/td[4]/div/img').click()
driver.find_element(By.XPATH, '//*[@id="ext-gen209"]').click()
driver.find_element(By.XPATH, '//*[@id="main"]/table/tbody/tr[2]/td[2]/table/tbody/tr[1]/td[11]/a').click()
driver.find_element(By.XPATH, '//*[@id="dateAnchor1_0"]/img').click()
driver.find_element(By.XPATH, '//*[@id="caldiv"]/table/tbody/tr/td/center/table[2]/tbody/tr[2]/td[4]/a').click()
driver.find_element(By.ID, 'dateAnchor2_0').click()
driver.find_element(By.XPATH, '//*[@id="caldiv"]/table/tbody/tr/td/center/table[2]/tbody/tr[2]/td[3]/a').click()

我怎样才能选择从今天起五天前的正确日期?

这是 HTML 代码:

<td align="left" nowrap="">
                <table border="0" cellspacing="0" cellpadding="0">
                    <tbody><tr>
                        
                        
                            <td align="right" nowrap="">
                                <div id="firstValue1_0" style="visibility: visible">
                                    <input type="text" name="values1" value="12/01/2021" size="11" maxlength="10" onkeypress="if(document.getElementById('firstCalendar1_0').style.visibility == 'visible' ) return processKeyPress(this); else return true;" onfocus="self.status='Date Format is ' + dtFormat;" onblur="self.status=' ';" onchange="if(document.getElementById('firstCalendar1_0').style.visibility == 'visible' ){if(isValidDate(this)) valueOnChange(0);} else valueOnChange(0);">&nbsp;

                                    <span id="firstValue3_0" style="display: none">% may be used as a wildcard character. </span>
                                
                                
                                    
                                    
                                        
                                        
                                            <div id="firstCalendar1_0" style="visibility: visible;display:inline">
                                        
                                    
                                

                                        <a id="dateAnchor1_0" name="dateAnchor1_0" onclick="dateSelect(0, 'values1');" style="vertical-align:middle"><img border="0" src="images/calendar.gif"></a>
                                    </div>
                                </div>

                            </td>
                            <td valign="center" nowrap="">
                                <div id="firstValue2_0" style="display: none">
                                    <span id="search0" class="search_sm" onclick="popupSearch('810', 'values1', 'Trade Date', 0)" onmouseover="doImgSwapOver('search_sm',0,true)" onmouseout="doImgSwapOut('search_sm',0,true)">
                                        <img name="search_sm" src="images/btn_search_sm.gif" width="24" height="22" alt="Search Trade Date" border="0" align="absmiddle">
                                    </span>
                                    <a name="searchAnchor0" id="searchAnchor0"></a>
                                </div>
                            </td>
                        
                        

                        
                        

                        <td valign="center" nowrap="">
                            
                                <div id="secondValue1_0" style="visibility: visible">
                            
                            
                                &nbsp;&nbsp;and&nbsp;&nbsp;
                                </div>
                        </td>
                        
                            <td valign="center" nowrap="">
                                
                                    <div id="secondValue2_0" style="visibility: visible">
                                
                                
                                        <input type="text" name="values2" value="02/03/2022" size="11" maxlength="10" onkeypress="return processKeyPress(this);" onfocus="self.status='Date Format is ' + dtFormat;" onblur="self.status=' ';" onchange="if(isValidDate(this)) valueOnChange(0);">
                                        &nbsp;
                                        <a id="dateAnchor2_0" name="dateAnchor2_0" onclick="dateSelect(0, 'values2');">
                                            <img border="0" src="images/calendar.gif">
                                        </a>
                                    </div>
                            </td>
                        
                        

                        

                        <td>
                        
                        </td>

                        
                        <td valign="center" nowrap="">

                        <!-- visibility -->
                        
                        <div id="tinMask_0" style="visibility:hidden">
                        

                        <!-- Tin Mask Selection Box -->
                        <table>
                            <tbody><tr>
                                <td>
                                    <!-- Previously set to SHOW -->
                                    

                                    <!-- Previously set to HIDE  -->
                                    
                                        

                                        <!-- Not previously set -->
                                        
                                            <!-- Tin Privileges -->
                                            
                                            <!-- No Tin Privileges -->
                                            
                                                <select name="tinMaskFlgs" onchange="flagOnChange(0,document.forms[0].tinMaskFlgs,document.forms[0].columnFilterTinMaskFlgs);">
                                                    <option value="false">Show Tin Values</option>
                                                    <option value="true" selected="">Hide Tin Values </option>
                                                </select>
                                            
                                         <!-- true -->
                                     <!-- false -->
                                </td>
                            </tr>
                        </tbody></table>
                        </div>
                        </td>

                    </tr>
                </tbody></table>
            </td>

【问题讨论】:

    标签: python html selenium xpath datepicker


    【解决方案1】:
    driver.find_element(BY_NAME,"values2").send_keys("02/03/2022")
    

    您可以尝试将键发送到名称为 values2 的输入标签。

    【讨论】:

    • 很遗憾,它没有用。不过谢谢!
    【解决方案2】:

    我想出了答案。我必须先清除值。

    import calendar
    import datetime
    from datetime import date
    from datetime import datetime, timedelta
    import time
    from selenium import webdriver
    from selenium.webdriver.chrome.service import Service
    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.common.exceptions import TimeoutException
    
    date_element = driver.find_element(By.NAME, 'values2')
    date_element.clear()
    five_days_prior = datetime.now() - timedelta(5)
    end_date = five_days_prior.strftime("%m/%d/%Y")
    date_element.send_keys(end_date)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-19
      • 1970-01-01
      • 2023-02-07
      • 1970-01-01
      • 2020-03-02
      • 2020-02-05
      相关资源
      最近更新 更多