So I want to use format to manipulate with timepicker in selenium python.
I have following variable:
time = Element('xpath=//li[contains(text(),"{}:{} PM")]')
And then I try to use format to located the element:
def select_ten_thirty_time(self):
self.time_picker.wait_element_to_be_clickable().click()
self.time.format(10, 30).wait_element_to_be_clickable().click()
But I get this error:
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: //li[contains(text(),"{}:{} PM")]
EDIT => Added HTML
<li class="react-datepicker__time-list-item react-datepicker__time-list-item--selected">10:30 PM</li>
Thank you for your help!
CodePudding user response:
You need to write a xPath
it should work for both AM
and PM
times.
//li[contains(text(),"10:30 AM") or contains(text(),"10:30 PM")]
Parameterize the time in xPath
//li[contains(text()," time " AM") or contains(text()," time " PM")]
Code:
time = '10:30'
wait.until(EC.element_to_be_clickable((By.XPATH, "//li[contains(text()," time " AM") or contains(text()," time " PM")]")))
CodePudding user response:
Please use f string like this :
hour = '10'
sec = '30'
time = driver.find_element_by_xpath(f"//li[contains(text(),'{hour}:{sec}') and contains(text()='PM')]")
You can have same for AM
or PM
.