0

I'm having some trouble finding elements with Selenium in Python, it works fine for every element on all other websites I have tested yet on a game website it can only find certain elements.

Here is the code I'm using:

from selenium import webdriver
import time

driver = webdriver.Chrome("./chromedriver")
driver.get("https://www.jklm.fun")

passSelf = input("Press enter when in game...")
time.sleep(1)
syllable = driver.find_element_by_xpath("/html/body/div[2]/div[2]/div[2]/div[2]/div").text
print(syllable)

Upon running the code, the element /html/body/div[2]/div[2]/div[2]/div[2]/div isn't found. In the image you can see the element it is trying to find:

Element the code is trying to find

However running the same code but replacing the XPath with something outside of the main game (for example the room code in the top right) it successfully finds the element:

Output of the code being run on a different element

I've tried using the class name, name, selector and XPath to find the original element but no prevail the only things I can think that are affecting it is that:

  1. The elements are changing periodically (not sure if this affects it)
  2. The elements are in the "Canvas area" and it is somehow blocking it.

I'm not certain whether these things matter as I'm new to using selenium any help is appreciated. The website the game is on is https://www.jklm.fun/ if you want to have a look through the elements

2 Answers 2

1

Element you are trying to access is inside an iframe. Switch to the frame first like this

driver.switch_to_frame(driver.find_element_by_xpath("//div[@class='game']/iframe[contains(@src,'jklm.fun')]"))
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much! Whilst trying to find the answer myself I did stumble across iframes but was a bit overwhelmed
I know this is a really old question and I'm not sure if it's against any sort of rule to post on a question this old, but I'm interested how you found the iframe? I've tried doing some research but I don't understand how you got that string in the "find element by xpath".
0
driver.get("https://jklm.fun/JXUS")
WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.XPATH, "//button[@class='styled']"))).click()
time.sleep(10)
driver.switch_to.frame(0)
while True:
    Get_Text = driver.find_element_by_xpath("//div[@class='round']").text
    print(Get_Text)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.