Selenium选择器无效

1tuwyuhd  于 2022-12-18  发布在  其他
关注(0)|答案(1)|浏览(185)

嗨,我不是一个很强的程序员,但你能帮我解决我的问题吗,我找不到解决方案。一些变化发生在 selenium 的最后一个relize,我的机器人返回错误。这是我的代码

page_ig = input("Enter page username: ")
browser.get(f"https://www.instagram.com/{page_ig}")
sleep_for_period_of_time()

followers_link = browser.find_element(By.XPATH, "//ul/li[2]/a")
followers_link.click()
sleep_for_period_of_time()

num_follow = input("How many person you want to follow:")

while(True):
    try:
        i = 0
        list_of_followers = browser.find_element(By.XPATH, '//button/div/div[contains(text()  "Follow")]')
        for person in list_of_followers:
            if person.text == "Follow":
                person.click()
                print("Followed!")
                i += 1
                print(i)
                sleep_for_period_of_time()
            else:
                pass
            if i >= int(num_follow):
                break

它返回错误:

Message: invalid selector: Unable to locate an element with the xpath expression //button/div/div[contains(text()  "Follow")] because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//button/div/div[contains(text()  "Follow")]' is not a valid XPath expression.
  (Session info: chrome=108.0.5359.98)
Stacktrace:
#0 0x555b36ed02a3 <unknown>
#1 0x555b36c8ef77 <unknown>
#2 0x555b36c929c8 <unknown>
#3 0x555b36c92792 <unknown>
#4 0x555b36c92a9c <unknown>

或者我添加了一些代码更改,它返回超出范围的列表

g2ieeal7

g2ieeal71#

这不是有效的XPath表达式'//button/div/div[contains(text() "Follow")]'
也许应该是

'//button/div/div[contains(text(), "Follow")]'

因此,Selenium代码行将为:

list_of_followers = browser.find_element(By.XPATH, '//button/div/div[contains(text(), "Follow")]')

相关问题