Cassano
Cassano

Reputation: 323

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:

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?

Upvotes: 1

Views: 1031

Answers (1)

STerliakov
STerliakov

Reputation: 7973

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']")))

Upvotes: 1

Related Questions