【发布时间】:2020-05-21 19:35:17
【问题描述】:
已解决:我正在尝试制作一个随机回答 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
【问题讨论】:
-
choices是什么?您没有在代码中定义它。 -
@Charles 你说得对,我忘了复制那部分代码,我将对其进行编辑,谢谢