Using aria-label to locate and click an element with Python3 and Selenium
Using the aria-label
property you can try the following xpath
:
driver.find_element_by_xpath("//div[@aria-label='Any time']/div[@class='mn-hd-txt' and text()='Any time']");
OR
driver.find_element_by_xpath("//div[@aria-label='Any time']/div[@class='mn-hd-txt'][text()='Any time']");
If using aria-label
property is not a mandatory requirement you can use the following:
driver.find_element_by_xpath("//div[@class='hdtb-mn-hd']/div[@class='mn-hd-txt' and text()='Any time']");
OR
driver.find_element_by_xpath("//div[@class='hdtb-mn-hd']/div[@class='mn-hd-txt'][text()='Any time']");
So, I was just wrestling with this for the last couple days, and it was proving to be a huge headache. The aria-label was basically the only reliable attribute, and the xpath solution was not working for me.
On a whim, I tried using:
driver.find_elements_by_css_selector("[aria-label=XXXX]")
where XXXX was the aria labels that I was searching for in single quotes (e.g. "[aria-label='More']"). Worked like a charm.
All this to say, try using the css selector. It just works.