AomineDaici
AomineDaici

Reputation: 763

Selecting element with selenium python issue

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">

Upvotes: 2

Views: 113

Answers (1)

Sameer Arora
Sameer Arora

Reputation: 4507

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()

Upvotes: 2

Related Questions