1

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!

3
  • how do you set its value? show us your code Commented Jul 30, 2016 at 5:11
  • The method I tried: role_name = driver.find_element_by_css_selector('input[ng-model="selectedRole.roleName"]') 1. role_name.send_keys("alvin") 2. self.driver.execute_script('arguments[0].setAttribute("value","alvin");',role_name), both are not working. BTW, I can get the element value by using role_name.get_attribute("value") Commented Jul 30, 2016 at 5:26
  • @Alvin Did you tried before set value to wait until element visible using WebDriverWait..?? Commented Jul 30, 2016 at 6:53

3 Answers 3

1

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")
Sign up to request clarification or add additional context in comments.

3 Comments

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");',roleInput), still no lucky
@Alvin I wonder if this input is intentionally hidden, or is this just a timing issue?
I add an image link for the input. is it because the input control implemented by angularjs style?
0

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...:)

4 Comments

Thank you, @Saurabh! I tried your method, still no lucky, I am wondering if selenium fully support angularjs applications?
What do you mean still no lucky?? Is there any exception?? Please share it
I got a "TimeoutException" error with blank message
did you look for frame or iframe??? Make sure this element is not inside any frame or iframe...
0

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

Comments

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.