简体   繁体   English

不是一个有效的 XPath 表达式试图使用 div aria label 包含

[英]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.我正在尝试查找包含“Following”的 div aria-label,但出现以下错误。

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".在“关注”之后,@AfifRah88504204 会随着我使用不同的链接而改变,所以我只想找到包含文本“关注”的 div aria-label。

What am I doing wrong?我究竟做错了什么?

There's a typo in given xpath, which is invalid due to missing parenthesis.给定的 xpath 中有一个错字,由于缺少括号而无效。 The following should work:以下应该有效:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM