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">
asked Apr 1, 2020 at 4:07
AomineDaici
7731 gold badge5 silver badges14 bronze badges
1 Answer 1
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()
answered Apr 1, 2020 at 5:07
Sameer Arora
4,5073 gold badges12 silver badges20 bronze badges
Sign up to request clarification or add additional context in comments.
5 Comments
AomineDaici
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:Sameer Arora
@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
AomineDaici
You typed the "driver" wrong in 2 places, (browser, 20), browser.switch_to.frame, can you correct ?
AomineDaici
Thank you very much for your answer and interest
Sameer Arora
@AomineDaici yeah i made a mistake there, have updated my answer. Thanks!
Explore related questions
See similar questions with these tags.
default