currentmonth = driver.findelement(By.CLASSNAME,'monthselect').text currentyear =…
- User
current_month = driver.find_element(By.CLASS_NAME,'monthselect').text current_year = driver.find_element(By.CLASS_NAME,'yearselect').text
This code produces the lists of all available months and years in calendar. But I need to have only the selected month and year in that variables
- Assistant
To get only the selected month and year in the
current_monthandcurrent_yearvariables, you can modify the code as follows:current_month = driver.find_element(By.CLASS_NAME, 'monthselect').get_attribute('value') current_year = driver.find_element(By.CLASS_NAME, 'yearselect').get_attribute('value')Instead of using the
.textmethod, you can use the.get_attribute('value')method to retrieve the selected value from the corresponding input field. This will give you the selected month and year. - User
Using selenium select day 25 that is located either in class "available" or "weekend available" in td tag
- Assistant
To select the day 25 from the calendar, which could be in the class "available" or "weekend available" within a
<td>tag, you can use the following code:day_25_element = driver.find_element(By.XPATH, "//td[contains(@class, 'available') or contains(@class, 'weekend available')][contains(text(), '25')]") day_25_element.click()Here's how it works:
- The
find_elementmethod is used to locate the element on the page. - The
By.XPATHlocator is used to find the element using an XPath expression. - The XPath expression used is
//td[contains(@class, 'available') or contains(@class, 'weekend available')][contains(text(), '25')].- The
contains(@class, 'available') or contains(@class, 'weekend available')checks for the elements that have either "available" or "weekend available" in their class attribute. - The
[contains(text(), '25')]further narrows down the elements that contain the text "25".
- The
- Once the element is found, the
click()method is called to select the day 25.
- The
- User
This code is working driver.find_element(By.XPATH, "//td[contains(@class, 'available') or contains(@class, 'weekend available')][contains(text(), '25')]").click()
But it selected all dates from initial to 25. But I need only select day 25
- Assistant
Apologies for the confusion. To select only the day 25 without selecting any other dates, you can modify the XPath expression slightly by including an
andcondition to check for an exact match with the text "25". Here's the modified code:day_25_element = driver.find_element(By.XPATH, "//td[contains(@class, 'available') or contains(@class, 'weekend available')][text()='25']") day_25_element.click()This updated XPath expression,
[text()='25'], ensures that only the<td>element with the exact text "25" is selected, rather than selecting any element that contains "25" within its text. - User
InvalidSelectorException Traceback (most recent call last) Cell In[41], line 1 ----> 1 driver.find_element(By.XPATH, 2 "//td[contains(@class, 'available') or contains(@class, 'weekend available')][contains(text()='20')]").click()
File ~\AppData\Local\anaconda3\Lib\site-packages\selenium\webdriver\remote\webdriver.py:741, in WebDriver.find_element(self, by, value) 738 by = By.CSS_SELECTOR 739 value = f'[name="{value}"]' --> 741 return self.execute(Command.FIND_ELEMENT, {"using": by, "value": value})["value"]
File ~\AppData\Local\anaconda3\Lib\site-packages\selenium\webdriver\remote\webdriver.py:347, in WebDriver.execute(self, driver_command, params) 345 response = self.command_executor.execute(driver_command, params) 346 if response: --> 347 self.error_handler.check_response(response) 348 response["value"] = self._unwrap_value(response.get("value", None)) 349 return response
- Assistant
Apologies for the mistake in the code. It seems there's an issue with the quotes within the XPath expression causing a syntax error. To resolve this issue, you can use single quotes inside the XPath expression. Here's the updated code:
day_25_element = driver.find_element(By.XPATH, "//td[contains(@class, 'available') or contains(@class, 'weekend available')][text()='25']") day_25_element.click()By using single quotes (
') inside the XPath expression, the expression should be valid now, and it should locate and select only the day 25 element without selecting any other dates.