2

I can't select the input field

I am using this code but I cannot select the input field.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome()
driver.get("https://10fastfingers.com/multiplayer")
input("Start : ")
a = "b"
inputfield = driver.find_element_by_xpath("//input[@type='text']")
inputfield.click()
while a == "b":
    try:
        word = driver.find_element_by_xpath("//span[@class='highlight']")
        inputfield.send_keys(word.text)
        inputfield.send_keys(Keys.SPACE)
    except:
        print("Finish")
        a = "c"

input field element;

<input type="text" autofocus="autofocus" autocapitalize="none" autocorrect="off">
0

1 Answer 1

2

As the page takes some time to load, you should apply explicit wait on the element so that the script waits until the element is present on the page.
You can do it like:

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

driver = webdriver.Chrome()
driver.get("https://10fastfingers.com/multiplayer")
input("Start : ")
a = "b"
driver.switch_to.frame(driver.find_element_by_tag_name('iframe'))
inputfield = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, "//input[@type='text']")))
inputfield.click()
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for the answer,(I've already added an input to wait for the page to load),unfortunately the code didn't work error code; Traceback (most recent call last): line 11, in <module> inputfield = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, "//input[@type='text']"))) line 80, in until raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message:
@AomineDaici Got the issue, there is an iframe which is present on your page, so you need to switch the driver to the iframe and then click on the element. Have modified my code. Please pick the latest code
You typed the "driver" wrong in 2 places, (browser, 20), browser.switch_to.frame, can you correct ?
Thank you very much for your answer and interest
@AomineDaici yeah i made a mistake there, have updated my answer. Thanks!

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.