Home > Software design >  Whenever I look for an element in a page, even if it exists, it is still coming up as nonexistent
Whenever I look for an element in a page, even if it exists, it is still coming up as nonexistent

Time:09-04

So I just started coding with Selenium and I made this program that goes onto the website JKLM.fun and it plays the game. Lately, I've been trying to type in the chat but I keep getting this error:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element

This is the code I am running:

chat = Driver.find_element(by=By.TAG_NAME, value="textarea")

And this is what I am trying to access:

enter image description here

And before you say to use XPATH or CSS selector or access the DIV above, none of those worked. If you need all my code I'll just put it below this. Can someone please please help me? I have been stuck on this forever!

import random
import time
import re
import keyboard

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

#Variables
Code = "JFEV"
Username = "Glitch BOT"
legitMode = False
totalLegitMode = False
lesslegitmode = False
botmode = False


Word = ""
Driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
usedWords = []
joinedGame = False
invalid = open("keyboardTest/invalid.txt", "a")
#Functions


def findWord(Prompt):
    notvalid = open("keyboardTest/invalid.txt").read().split('\n')
    global usedWords
    Words = open("keyboardTest/bigtosmall.txt").read().split("\n")
    bestword = ""
    for o in range(len(Words)):
        if Prompt.lower() in Words[o] and Words[o] not in usedWords and Words[o] not in notvalid:
            bestword = Words[o]
            usedWords.append(bestword)
            break

    # while not (Prompt.lower() in Word):
    #     Word = (random.choice(Words))
    #     if Prompt.lower() in Word and not (Word in usedWords):
    #         usedWords.append(Word)
    #         break
    if bestword == "":
        print('No Word Found For:', Prompt)
    return bestword


def joinServer():
    global Code
    if Code == "":
        Driver.get("https://jklm.fun")
        while Driver.current_url == "https://jklm.fun/":
            pass
        return
    else:
        Driver.get(f"https://jklm.fun/{Code}")
        return

def joinGame():
    global joinedGame
    while joinedGame == False:
        try:
            joinBox = Driver.find_element(by=By.CLASS_NAME, value="join")
            joinButton = joinBox.find_element(by=By.TAG_NAME, value="button")
            ActionChains(Driver).move_to_element(joinButton).click(joinButton).perform()
            joinedGame = True
        except:
            pass

#Code
joinServer()
OK = Driver.find_element(by=By.CLASS_NAME, value="line")
Driver.implicitly_wait(5)
usernameBox = OK.find_element(by=By.TAG_NAME, value="input")
Driver.implicitly_wait(5)
while True:
    if usernameBox.get_attribute("value") != "":
        time.sleep(0.1)
        usernameBox.send_keys(Keys.BACK_SPACE)
    else:
        usernameBox.send_keys(Username)
        usernameBox.send_keys(Keys.RETURN)
        break
iFrame = Driver.find_element(by=By.TAG_NAME, value="iframe")
Driver.switch_to.frame(iFrame)


joinGame()

print('JOINED THE GAME')
time.sleep(2)
print('AFTER DELAY')
try:
    chat = Driver.find_element(by=By.TAG_NAME, value="textarea")
    chat.clear()
except:
    print('DID NOT WORK!!!! LLLL')
print("DEFINED CHAT!")
chat.send_keys('Testing')
print("SAID TESTING!")
chat.send_keys(Keys.RETURN)
print("PRINTED IT OUT!")

while joinedGame == True:
    try:
        #time.sleep(0.3)
        joinBox = Driver.find_element(by=By.CLASS_NAME, value="join")
        if not joinBox.is_displayed():
            Player = Driver.find_element(by=By.CLASS_NAME, value="player")
            selfTurn = Driver.find_element(by=By.CLASS_NAME, value="selfTurn")      
            if Player.text == "" and selfTurn.is_displayed():
                Input = selfTurn.find_element(by=By.CLASS_NAME, value="styled")
                Prompt = Driver.find_element(by=By.CLASS_NAME, value="syllable").text
                print("Current Prompt is:",Prompt)
                guess = findWord(Prompt)
                print("The guess for that prompt is:", guess)
                if legitMode:
                    time.sleep(random.uniform(0.3,0.8))
                    for i in range(len(guess)):
                        time.sleep(random.uniform(0.01,.12))
                        Input.send_keys(guess[i])
                elif totalLegitMode:
                    time.sleep(random.uniform(0.2,1))
                    for i in range(len(guess)):
                        time.sleep(random.uniform(0.05,.14))
                        Input.send_keys(guess[i])
                elif lesslegitmode:
                    time.sleep(random.uniform(0.1,.6))
                    for i in range(len(guess)):
                        time.sleep(random.uniform(0.02,.11))
                        Input.send_keys(guess[i])
                else:
                    Input.send_keys(guess)
                Input.send_keys(Keys.RETURN)
                usedWords.append(guess)
                print("just used word:", guess)
                if not botmode:
                    time.sleep(.2)
                if selfTurn.is_displayed() and Driver.find_element(by=By.CLASS_NAME, value="syllable").text == Prompt:
                    # a = open("keyboardTest/invalid.txt").read().split('\n')
                    # if guess not in a: #if its not already in the list
                    invalid.write('\n')
                    invalid.write(guess) #if word didn't work, put it into invalid list then ill manually check it
                    invalid.close()
                    invalid = open("keyboardTest/invalid.txt", "a")
                        # guess = findWord(Prompt)
                        # print(guess)
        else:
            usedWords = []
            joinButton = joinBox.find_element(by=By.TAG_NAME, value="button")
            ActionChains(Driver).move_to_element(joinButton).click(joinButton).perform()
    except Exception as e:
        pass

CodePudding user response:

I figured it out. All you have to do is switch to parent frame before accessing chatbox. That is where it is located. It isn't located in the iframe.

Driver.switch_to.parent_frame() #check for commands

And you're good to go! (Don't forget to switch back to Iframe when done accessing chatbox)

  • Related