使用硒Xpath在Python中的日期选择器下拉菜单中找到元素?

问题描述

我想单击下拉日期选择器中的日期。发生错误无法找到元素。我尝试过:

monyear = driver.find_element_by_xpath('/html/body/div[7]/div[1]/table/tbody/tr[5]/td[1]').click()

element=webdriverwait(driver,30).until(ec.element_to_be_clickable((By.XPATH,"/html/body/div[7]/div[1]/table/tbody/tr[5]/td[1]")))

driver.execute_script("arguments[0].click();",element)

但是他们都没有为我工作。 请提出可能的解决方。这是dopBoxHTML代码和图像。

<div class="datepicker dropdown-menu" style="display: block; top: 311.969px; left: 627.406px;">
 <div class="datepicker-days" style="display: block;">
  <table class=" table-condensed">
   <thead>
    <tr>
     <th class="prev">‹</th>
     <th colspan="5" class="switch">August 2020</th>
     <th class="next">›</th>
    </tr>
    <tr>
     <th class="dow">Su</th>
     <th class="dow">Mo</th>
     ...........
     <th class="dow">Sa</th>
    </tr></thead>
  <tbody>
   ............
   <tr>
    <td class="day  active">23</td>
    <td class="day ">24</td>
    <td class="day ">25</td>
    <td class="day ">26</td>
    <td class="day ">27</td>
    <td class="day ">28</td>
    <td class="day ">29</td></tr>
   <tr>

enter image description here

我发现了这两个帧:

<iframe src="chrome-extension://kdfieneakcjfaiglcfcgkidlkmlijjnh/writer/index.html"></iframe>
<iframe class="ginger-banner-frame" style="width:385px;height:85px;" ng-src="https://cdn.gingersoftware.com/banners/default/chromeextension.writer.underTools.html" src="https://cdn.gingersoftware.com/banners/default/chromeextension.writer.underTools.html"></iframe>

这些可能是chrome扩展名。它们有效果吗?

解决方法

为什么不写绝对的xPath,为什么还可以更简短,更容易地调试相对xpth。像

date = 19 # You can choose any date
dateXpath = "//td[contains(@Class,'day') and text()="+str(date)+"]"


# Clicking on calendar input
driver.find_element_by_id('startDateToday').click()

# Clicking on Date
WebDriverWait(driver,30).until(EC.element_to_be_clickable((By.XPATH,dateXpath))).click()

# Bug with page,as after clicking on date calendar is not closing. So clicking outside
WebDriverWait(driver,30).until(
    EC.element_to_be_clickable((By.XPATH,"//label[contains(text(),'Total Rows')]"))).click()

注意:

  1. 我假设您的日期选择器与页面位于同一帧中。如果有单独的iFrame,则需要相应地进行处理。 Getting "Nosuch element Exception" in Selenium Though XPATH is correct. Not sure is this due to Shadow DOM. Please confirm

  2. 上述解决方案假设您要单击当前打开的月份/年份中的日期(在这种情况下为2020年8月)。如果要更改不同的月/年,则必须相应地标识各个元素并在代码中进行修改。