Using aria-label to locate and click an element with Python3 and Selenium
Asked Answered
C

3

23

I want to click or more respectively, expand the "Any time" button. I've tried to locate the element by class_name and xpath. The problem is that the class and xpath are the same for all three 'options'. So, I would like to select and click or expand on that element by using the aria-label. I found a couple of suggestions, but it didn't work for me. Most importantly, I try to do that in python 3. I also tried:

driver.find_element_by_xpath(""" //div*[@aria-label='Any Time'] """).click()

but it doesn't work.

Could anyone please help me? Many thanks in advance!

<div class="hdtb-mn-hd" aria-haspopup="true" role="button" tabindex="0" aria-label="Any country"><div class="mn-hd-txt">Any country</div><span class="mn-dwn-arw"></span></div>
<div class="hdtb-mn-hd" aria-haspopup="true" role="button" tabindex="0" aria-label="Any time"><div class="mn-hd-txt">Any time</div><span class="mn-dwn-arw"></span></div>
<div class="hdtb-mn-hd" aria-haspopup="true" role="button" tabindex="0" aria-label="All results"><div class="mn-hd-txt">All results</div><span class="mn-dwn-arw"></span></div>
Clem answered 10/10, 2017 at 14:54 Comment(0)
R
18

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']");
Rufford answered 10/10, 2017 at 15:18 Comment(3)
Thank you very much for your reply, @DebanjanB! All of your suggestions work, however, I still can't click on it, or more respectively expand it. I get the following error message: Message: unknown error: Element <div class="mn-hd-txt">...</div> is not clickable at point (306, 99). Other element would receive the click: <a class="q qs" href="/search?q=ISIS+OR+IS+OR+%22Islamic+State%22&amp;dcr=0&amp;source=lnms&amp;tbm=isch&amp;sa=X&amp;ved=0ahUKEwjYy56A4OrWAhWFDMAKHVG9BDAQ_AUICygC">...</a> Any ideas how to solve this problem? Thanks in advance!Clem
For Element is not clickable at point (x, y) see this Discussion / QARufford
For those trying to do the same for other things... Another example; tags = driver.find_element(By.XPATH,"//textarea[@aria-label='tags']") where 'tags' is aria-label="tags" (in the 'view source' of a page) in a 'textarea' element. You can swap 'textarea' with 'input' if it is an input field instead and so on.Covenantee
A
37

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.

Anoxemia answered 15/10, 2019 at 18:26 Comment(2)
You may need to quote this, namely find_element(s)_by_css_selector("element[property='foo']"). Using a div for example: ...selector("div[aria-label='XXXX']").Popeyed
Newer versions of selenium changed the syntax so it should be: driver.find_elements(By.CSS_SELECTOR, "[aria-label='XXX']") (added quotes in case the label contains spaces)Orientation
R
18

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']");
Rufford answered 10/10, 2017 at 15:18 Comment(3)
Thank you very much for your reply, @DebanjanB! All of your suggestions work, however, I still can't click on it, or more respectively expand it. I get the following error message: Message: unknown error: Element <div class="mn-hd-txt">...</div> is not clickable at point (306, 99). Other element would receive the click: <a class="q qs" href="/search?q=ISIS+OR+IS+OR+%22Islamic+State%22&amp;dcr=0&amp;source=lnms&amp;tbm=isch&amp;sa=X&amp;ved=0ahUKEwjYy56A4OrWAhWFDMAKHVG9BDAQ_AUICygC">...</a> Any ideas how to solve this problem? Thanks in advance!Clem
For Element is not clickable at point (x, y) see this Discussion / QARufford
For those trying to do the same for other things... Another example; tags = driver.find_element(By.XPATH,"//textarea[@aria-label='tags']") where 'tags' is aria-label="tags" (in the 'view source' of a page) in a 'textarea' element. You can swap 'textarea' with 'input' if it is an input field instead and so on.Covenantee
E
-1
import { By } from "selenium-webdriver";    

await driver.findElements(By.css("svg[aria-label='Hello']"));
Estoppel answered 27/1 at 19:39 Comment(1)
This questions was tagged with python. This is not Python code.Orientation

© 2022 - 2024 — McMap. All rights reserved.