I'm currently creating a selenium bot to create a bot for entering in kahoots. The problem is when i enter the code and I try to automatically write the nickname in the textfield the script gives me this error:
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)
The code of the script is the following:
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()
if name == "main": test_a()
And the textfield i wanna enter the text is the following:
<input name="nickname" type="text" placeholder="Nickname" maxlength="15" id="nickname" data-functional-selector="username-input" autocomplete="off" dir="ltr" value="jjj">
CodePudding user response:
According to error trace, your issue is not finding the element in page. If that element is not in an iframe, or in a shadow root element and the ID attribute is correct, you should be able to locate it with:
text_field = WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.ID, "nickname")))
You also need the following imports:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
And lastly, Selenium documentation can be found at https://www.selenium.dev/documentation/
It would be wise to wait out the rest of the elements you are trying to locate, to make sure they are loaded and interactable.
CodePudding user response:
As per the HTML provided:
<input name="nickname" type="text" placeholder="Nickname" maxlength="15" id="nickname" data-functional-selector="username-input" autocomplete="off" dir="ltr" value="jjj">
I don't see any such issues in the locator strategy you have used, i.e.
driver.find_element(By.ID, "nickname")
Solution
Ideally to send a character sequence to the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Using ID:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "nickname"))).click()
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#nickname"))).send_keys("vinci0000")
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='nickname']"))).send_keys("vinci0000")
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC