Python 已解决:selenium.common.exceptions.InvalidSelectorException:消息:无效选择器:指定了无效或非法的选择器

Python 已解决:selenium.common.exceptions.InvalidSelectorException:消息:无效选择器:指定了无效或非法的选择器,python,selenium,Python,Selenium,已解决:我正在尝试创建一个随机回答kahoot的脚本,一切正常,但当脚本必须单击“下一步”按钮时,我出现以下错误:“selenium.common.exceptions.InvalidSelectorException:消息:无效选择器:指定了无效或非法的选择器” 我试图搜索此错误,但没有显示与我的脚本相关的内容。 请帮帮我 import random from selenium import webdriver driver=webdriver.Firefox() driver.get("ka

已解决:我正在尝试创建一个随机回答kahoot的脚本,一切正常,但当脚本必须单击“下一步”按钮时,我出现以下错误:“selenium.common.exceptions.InvalidSelectorException:消息:无效选择器:指定了无效或非法的选择器” 我试图搜索此错误,但没有显示与我的脚本相关的内容。 请帮帮我

import random
from selenium import webdriver
driver=webdriver.Firefox()
driver.get("kahoot link")
choices= ['//*[@id="challenge-game-router"]/main/div[2]/div[1]/span', '//*[@id="challenge-game-router"]/main/div[2]/div[2]/span', '//*[@id="challenge-game-router"]/main/div[2]/div[3]', '//*[@id="challenge-game-router"]/main/div[2]/div[4]']
try:
    sleep(5)
    nickname= driver.find_element_by_name('nickname')
    nickname.send_keys(username)
    enter = driver.find_element_by_xpath('//*[@id="challenge-game-router"]/main/section/div[1]/form/button').click()
finally:
    pass


for c in range(0, 3):
    try:
        driver.implicitly_wait(20)
        choice = driver.find_element_by_xpath(random.choice(choices))
        choice.click()
        driver.implicitly_wait(20)
        next = driver.find_element_by_xpath('/html/body/div/div/div/div/main/button')
        next.click()
    finally:
        pass

您试图在这里按类名查找元素,但提供了xpath到
find\u element\u by\u class\u name
函数,这就是它抛出无效选择器异常的原因

next = driver.find_element_by_class_name('/html/body/div/div/div/div/main/button')

您应该提供一个有效的类名,或者改用
find\u element\u by\u xpath

我通过使用Firefox而不是Google\u Chrome复制xpath解决了我的问题。

谢谢,但现在我遇到了以下错误:selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法定位元素:{“方法”:“xpath”“选择器”:“/html/body/div/div/div/main/button”}然后可能需要更正xpath
/html/body/div/div/div/div/main/button
。我试图检查和复制xpath,得到了这个:'/*[@id=“challenge game router”]/main/button',但我仍然得到了相同的错误(是否有其他方法得到xpath,我做错了?)好的,为了确保您的xpath
/*[@id=“challenge game router”]/main/button
是否正确,请在inspect元素查找栏中输入它,看看它是否突出显示了按钮?不,不是高位,我怎么可能复制了错误的xpath?有什么
选项?您没有在代码中定义它。@Charles您是对的,我忘了复制代码的这一部分,我将编辑它,谢谢