Reputation: 1
I'm having trouble having selenium locate an element on a website (gleam to be exact). Ideally I would like the driver to send keys to an input field, but selenium won't locate it for some reason.
I've already tried locating by ID, Xpath and by name. Any suggestions on how to locate this element?
Here's the html:
<input
id="contestant[name]"
name="name"
ng-model-options="{ debounce: 300 }"
ng-model="contestantState.form.name"
ng-pattern=".*"
placeholder="Alice Smith" required=""
style="width: 246px"
type="text"
class="ng-empty
ng-invalid
ng-invalid-required
ng-valid-pattern
ng-dirty
ng-valid-parse
ng-touched"
>
Upvotes: 0
Views: 130
Reputation: 193078
To send a character sequence to the desired element as the element is an Angular element so you need to induce WebDriverWait for the element to be clickable and you can use either of the following Locator Strategies:
Using CSS_SELECTOR
:
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.ng-empty.ng-invalid.ng-invalid-required.ng-valid-pattern.ng-dirty.ng-valid-parse.ng-touched[id=\"contestant[name]\"]"))).send_keys("ml2017")
Using XPATH
:
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='ng-empty ng-invalid ng-invalid-required ng-valid-pattern ng-dirty ng-valid-parse ng-touched' and @id=\"contestant[name]\"]"))).send_keys("ml2017")
Upvotes: 0
Reputation: 459
Try one of these
By.CssSelector("[id*='contestant']")
By.CssSelector("[ng-model='contestantState.form.name']")
By.CssSelector("[name='name']")
Upvotes: 1
Reputation: 33384
Use WebDriverWait
to handle the dynamic elements on Web Page.Try following code.
If this code not work check whether your input element inside any iframe.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium import webdriver
inputelement=WebDriverWait(driver,40).until(expected_conditions.element_to_be_clickable((By.ID,'contestant[name]')))
inputelement.send_keys("Apple")
Upvotes: 0