selenium bot在您按下bot时不起作用



我目前正在创建一个硒机器人,以创建一个进入kahoots的机器人。问题是,当我输入代码并尝试在文本字段中自动写入昵称时,脚本会给我这个错误:

Traceback (most recent call last):
File "/Users/marcovinciguerra/Github/Python/Selenium _Kahoot_bot/a.py", line 32, in <module>
test_a()
File "/Users/marcovinciguerra/Github/Python/Selenium _Kahoot_bot/a.py", line 24, in test_a
driver.find_element(By.ID, "nickname").click()
File "/Users/marcovinciguerra/opt/anaconda3/envs/envpython/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 976, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "/Users/marcovinciguerra/opt/anaconda3/envs/envpython/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/Users/marcovinciguerra/opt/anaconda3/envs/envpython/lib/python3.10/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="nickname"]"}
(Session info: chrome=104.0.5112.79)

脚本的代码如下:

def test_a():
driver.get("https://kahoot.it/")
driver.set_window_size(1512, 945)
driver.find_element(By.ID, "game-input").click()
driver.find_element(By.ID, "game-input").send_keys("14988")
driver.find_element(By.ID, "game-input").send_keys(Keys.ENTER)
driver.find_element(By.ID, "nickname").click()
driver.find_element(By.ID, "nickname").send_keys("vinci0000")
driver.find_element(By.CSS_SELECTOR, ".sc-jJEJSO").click()
element = driver.find_element(By.CSS_SELECTOR, ".sc-jJEJSO")
actions = ActionChains(driver)
actions.move_to_element(element).perform()

如果name==">main":测试_((

我想输入的文本字段如下:

<input name="nickname" type="text" placeholder="Nickname" maxlength="15" id="nickname" data-functional-selector="username-input" class="sc-fWSCIC hEgquS" autocomplete="off" dir="ltr" value="jjj">

根据错误跟踪,您的问题是在页面中找不到元素。如果该元素不在iframe中,或者不在影子根元素中,并且ID属性是正确的,那么您应该能够使用来定位它

text_field = WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.ID, "nickname")))

您还需要以下导入:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

最后,Selenium文档可以在https://www.selenium.dev/documentation/

明智的做法是等待您试图定位的其余元素,以确保它们已加载并可交互。

根据提供的HTML:

<input name="nickname" type="text" placeholder="Nickname" maxlength="15" id="nickname" data-functional-selector="username-input" class="sc-fWSCIC hEgquS" autocomplete="off" dir="ltr" value="jjj">

我在您使用的定位器策略中没有看到任何此类问题,即

driver.find_element(By.ID, "nickname")

解决方案

理想情况下,要向元素发送字符序列,您需要诱导WebDriver等待element_to_be_clickle((,并且您可以使用以下定位器策略之一

  • 使用ID

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "nickname"))).click()
    
  • 使用CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#nickname"))).send_keys("vinci0000")
    
  • 使用XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='nickname']"))).send_keys("vinci0000")
    
  • 注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

相关内容

  • 没有找到相关文章

最新更新