Home > Back-end >  Not a valid XPath expression trying to use div aria label contains
Not a valid XPath expression trying to use div aria label contains

Time:12-25

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"  data-testid="1354561188005347337-unfollow" style=""><div dir="auto"  style=""><span ><span >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?

CodePudding user response:

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