Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Not a valid XPath expression trying to use div aria label contains

I’m trying to locate div aria-label containing "Following" but I get the below error.

Error:

selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: Unable to locate an element with the xpath expression //div[contains(@aria-label, 'Following' and @role='button'] because of the following error:   
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//div[contains(@aria-label, 'Following' and @role='button']' is not a valid XPath expression.

Code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

unfollowuser = wait.until(EC.element_to_be_clickable((By.XPATH, "//div[contains(@aria-label, 'Following' and @role='button']")))

Element I’m trying to locate:

<div aria-label="Following @AfifRah88504204" role="button" tabindex="0" class="css-18t94o4 css-1dbjc4n r-1niwhzg r-1ets6dv r-sdzlij r-1phboty r-rs99b7 r-2yi16 r-1qi8awa r-1ny4l3l r-ymttw5 r-o7ynqc r-6416eg r-lrvibr" data-testid="1354561188005347337-unfollow" style=""><div dir="auto" class="css-901oao r-1awozwy r-18jsvk2 r-6koalj r-18u37iz r-16y2uox r-37j5jr r-a023e6 r-b88u0q r-1777fci r-rjixqe r-bcqeeo r-q4m81j r-qvutc0" style=""><span class="css-901oao css-16my406 css-bfa6kz r-poiln3 r-a023e6 r-rjixqe r-bcqeeo r-qvutc0"><span class="css-901oao css-16my406 r-poiln3 r-bcqeeo r-qvutc0">Following</span></span></div></div>

After "Following" there is @AfifRah88504204 which changes as I use different links so I want to only locate the div aria-label containing the text "Following".

What am I doing wrong?

>Solution :

There’s a typo in given xpath, which is invalid due to missing parenthesis. The following should work:

unfollowuser = wait.until(EC.element_to_be_clickable(
    (By.XPATH, "//div[contains(@aria-label, 'Following') and @role='button']")))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading