Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

In the target webpage, there is an angularjs input element:

<input type="text" class="form-control ng-pristine ng-valid ng-valid-maxlength ng-touched" placeholder="Role name" ng-model="selectedRole.roleName" maxlength="50">

enter image description here And i can locate the element using selenium(python) by using (By.CSS_SELECTOR,'input[ng-model="selectedRole.roleName"]'), but cannot set its value, can anybody help on this, thanks in advance!

share|improve this question
    
how do you set its value? show us your code – Yu Zhang Jul 30 at 5:11
    
The method I tried: role_name = driver.find_element_by_css_selector('input[ng-model="selecte‌​dRole.roleName"]') 1. role_name.send_keys("alvin") 2. self.driver.execute_script('arguments[0].setAttribute("value‌​","alvin");',role_na‌​me), both are not working. BTW, I can get the element value by using role_name.get_attribute("value") – Alvin Jul 30 at 5:26
    
@Alvin Did you tried before set value to wait until element visible using WebDriverWait..?? – Saurabh Gaur Jul 30 at 6:53

Once you've located the input element, just send the keys to it:

role_name = driver.find_element_by_css_selector('input[ng-model="selectedRole.roleName"]')
role_name.send_keys("test")
share|improve this answer
    
I tried the method before, and got below error:Message: element not visible (Session info: chrome=51.0.2704.103) (Driver info: chromedriver=2.21.371459 (36d3d07f660ff2bc1bf28a75d1cdabed0983e7c4),platform=Windows NT 6.1 SP1 x86_64), And i also tried to use execute_script: self.driver.execute_script('arguments[0].setAttribute("value‌​","alvin");',roleInp‌​ut), still no lucky – Alvin Jul 30 at 5:21
    
@Alvin I wonder if this input is intentionally hidden, or is this just a timing issue? – alecxe Jul 30 at 5:29
    
I add an image link for the input. is it because the input control implemented by angularjs style? – Alvin Jul 30 at 5:48

I think you need to wait before send_keys using WebDriverWait until element to be visible as below :

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

wait = WebDriverWait(driver, 20)
role_name = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR,'input[ng-model="selectedRole.roleName"]')))

#now go for set value
role_name.send_keys("alvin")

If you are still unable to set value try using execute_script as below :

driver.execute_script("arguments[0].value = 'alvin'", role_name)

Hope it helps...:)

share|improve this answer
    
Thank you, @Saurabh! I tried your method, still no lucky, I am wondering if selenium fully support angularjs applications? – Alvin Jul 30 at 7:26
    
What do you mean still no lucky?? Is there any exception?? Please share it – Saurabh Gaur Jul 30 at 7:33
    
I got a "TimeoutException" error with blank message – Alvin Jul 30 at 7:41
    
did you look for frame or iframe??? Make sure this element is not inside any frame or iframe... – Saurabh Gaur Jul 30 at 7:42

You can simply access and set value using the below code.

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

driver.find_element_by_xpath("//input[@ng-model = 'selectedRole.roleName']")
WebDriverWait(browser, 60).until(EC.presence_of_element_located((By.XPATH, "//input[@ng-model = 'selectedRole.roleName']"))).send_keys('Your Value')
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.