Locate element with selenium Xpath for date picker drop-down menu in Python?

﹥>﹥吖頭↗ 提交于 2021-02-08 03:35:09

问题


I wanted to click on the date in the dropdown datepicker. Bus the error occurs Unable to Locate Element. I tried :

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

and

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)

But none of them worked for me. Please suggest the possible solutions for it. Here is the HTML code and image of the dopbox.

<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>

I found these two frames:

<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>

These might be of chrome extensions. Do they have any effect?


回答1:


Instead of writing absolute xPath why can you right more short and easy to debug relative xpth. Some thing like

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()

Note :

  1. I am assuming your date picker is in same frame as your page. If there is a separate iFrame you need to handle accordingly. Getting "Nosuch element Exception" in Selenium Though XPATH is correct. Not sure is this due to Shadow DOM. Please confirm

  2. Above solution is with assumption that you want to click date from current open month/year ( In this case August, 2020). In case you want to change different Month/ year, you have to identify respective elements accordingly and make amendments in your code.



来源:https://stackoverflow.com/questions/63549816/locate-element-with-selenium-xpath-for-date-picker-drop-down-menu-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!