0

I am using Python Selenium trying to get some data from a website and need to change the day of a date.

enter image description here

I tried the following: Get the table with all dates and iterate through all tds. If the right day appears click. Unfortunately that does not work. It prints the correct numbers but it does not click on the one it should or any.

day_table = depar_date.find_element_by_xpath("/html/body/div[8]/section/div/div/div[2]/div/table/tbody")
day_table.click()

for row in test.find_elements_by_css_selector('tr'):
    for cell in row.find_elements_by_tag_name('td'):
        print(cell.text)
        if cell.text == "15":
            cell.click()

I get the following error message:

StaleElementReferenceException: stale element reference: element is not attached to the page document

I also see that for the selected day aria-pressed = "true", is there a way to set this "true" for the correct day?

many thanks for any help.

1
  • Can you please share the url. You don't need a loop to select the date, you can do it with a simple xpath. Commented Jul 13, 2019 at 14:30

1 Answer 1

0

I guess you need to perform the action on the button itself, not the "tr" element because the event listener is on the button. Could you try to do the following and let me know what happens:

 depar_date.find_element_by_xpath("//td/button[text()="15"]").click()
Sign up to request clarification or add additional context in comments.

5 Comments

many thanks. I tried, unfortunately I get: NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//td/button[text()="15"]"}
many thanks! That works, yet if I replace "15" with a variable saved as a string it does not anymore.... day = str(14) day_table2 = depar_date.find_element_by_xpath('//td//*[contains(@aria-label,day)]').click()
You can use escape character for this. Please mark it as answered.
what you mean by use "escape character"? Will do! Thanks for your help!!
To use the xpath as the following:"//td//*[contains(@aria-label,\"15\")]" or use single quotes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.