selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: An invalid or illegal selector was specified

Question:

I am trying to make a script that answers a kahoot randomly, everything is working fine but when the script has to click on the "next" button I get this error:'selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: An invalid or illegal selector was specified'
I tried searching for this error but nothing that related to my script showed up.

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
Asked By: bastostam 1

||

Answers:

You are trying to find element by class name here, but providing xpath to find_element_by_class_name funtion, which is why it throws invalid selector exception.

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

Either you should provide a valid classname or use find_element_by_xpath instead.

Answered By: Sodium

I solved my problem by copying the Xpath with Firefox instead of Google_Chrome.

Answered By: bastostam 1
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.