How can I use Selenium to navigate a popup calendar using Python?
This is a hobby project, so I can get to know Selenium better.
I have a list of dates. I want to get the movie time for each of these dates. I can load the theater site and calendar without any problem, but when I get to a certain day I get a lead. How can I use Selenium to click a day on a calendar?
Here's what I have so far. Open your browser, load the page, click and open the calendar:
from selenium import webdriver
browser = webdriver.Firefox()
url = "https://www.amctheatres.com/movie-theatres/los-angeles/amc-promenade-16"
browser.get(url)
browser.find_element_by_xpath(".//*[@id='showtime-date']").click()
browser.implicitly_wait(3)
My three attempts have not worked so far.
Attempt 1: When I try to find a link by text, I get:
NoSuchElementException: Message: Unable to find element
elem = browser.find_element_by_link_text('28').click()
Attempt 2:
This xpath finds the element and the click method works, but I want to be able to select the day by the text of the string
xpath = ".//*[@id='theatre-showtimes-202']/div/div[2]/div[1]/div/div/div[1]/table/tbody/tr[5]/td[5]"
elem = browser.find_element_by_xpath(xpath).click()
Attempt 3:
Finding an element using xpath and text works, but then the click method does nothing
xpath_by_text = '//td[contains(text(), "28")]'
elem = browser.find_element_by_xpath(xpath_by_text).click()
What can I do to find the day of the calendar by the day number and then click on it?
source to share
Attempt 1: When I try to find a link by text
This does not work because the locator is searched for "by link text" only using elements a
. You need an item td
.
Attempt 2 ...
As you noted, this is not what you want to do. You need to find an element by text.
Attempt 3
This is very close to the desired result, but as you can see there are two elements with text 28
. You find the first one td
(because according to the WebDriver spec : "All element placement strategies should return elements in the order they appear in the current document") that is disabled (which explains why the click does nothing) and comes from the previous months.
Instead, search for the element td
by text and also skip elements that contain disabled
in the class attribute value:
//td[. = "28" and not(contains(@class, "disabled"))]
Demo from Chrome console:
> $x('//td[. = "28"]')
[<td class="old disabled day">28</td>, <td class="day">28</td>]
> $x('//td[. = "28" and not(contains(@class, "disabled"))]')
[<td class="day">28</td>]
source to share
You can try this solution
WebDriver driver = new FirefoxDriver();
driver.get("https://www.amctheatres.com/movie-theatres/los-angeles/amc-promenade-16");
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
driver.findElement (By.xpath ("html / body / DIV [3] / case [2] / case [1] / case / case / case / case [2] / case [1] / DIV / entry. [1 ] ")) press (); driver.findElement (By.xpath ("HTML / body / DIV [3] / case [2] / case [1] / case / case / case / case [2] / case [1] / case / case / case [ 1] / table / TBODY / tr [5] / td [5] ")) press () ;.
source to share