Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 685
bastostam 1 Avatar asked Nov 21 '25 15:11

bastostam 1


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.

like image 137
Sodium Avatar answered Nov 24 '25 06:11

Sodium



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!