Python selenium xpath descendant

Viewed 711

Does anyone know how to get a button with:

svg[@aria-label='Like']

Here is the screenshot: enter image description here

I tryed various combinations ... nothing worked. I worked around with:

//button[@class='wpO6b ']

But i don't want to use it anymore. So my trys:

//button[@class='wpO6b ']/descendant::svg[@aria-label='Like']
//button[@class='wpO6b' and descendant::svg[@aria-label='Like']]
//button[@class='wpO6b ']//svg[@aria-label='Like']

Thanks for reading!

Edit: I want to click the button-element.

3 Answers

Try the below xpath in your code -

driver.find_element_by_xpath('//*[local-name()="svg" and @aria-label="Like"]/parent::span/parent::div/parent::button').click()

Hope it will detect the button. let me know the outcome.

Why these one don't work :

//button[@class='wpO6b ']/descendant::svg[@aria-label='Like']
//button[@class='wpO6b ']//svg[@aria-label='Like']

You select the svg element not the button. You have to put the svg part inside a predicate.

//button[@class='wpO6b' and descendant::svg[@aria-label='Like']]

This one is almost good, but since there is a space in the class attribute of the button element it won't work. It can be fixed accordingly with a contains function which is a more secure option here :

//button[contains(@class,'wpO6b') and descendant::svg[@aria-label='Like']]

Another way to select the button (using ancestor axis and more robust predicates) :

//svg[@aria-label="Like" and @fill="#262626"]/ancestor::button[1][contains(@class,"wpO6b")]

If you're dealing with namespaces, use the following expression :

//*[local-name()='button'][contains(@class,'wpO6b') and descendant::*[local-name()='svg'][@aria-label='Like']]

You can write xpath:

//svg[@aria-label='Like']/ancestor::button[1]
Related