如何使用selenium(python)

时间:2016-07-30 05:00:58

标签: python angularjs selenium

在目标网页中,有一个angularjs输入元素:

<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 我可以使用(By.CSS_SELECTOR,'input[ng-model="selectedRole.roleName"]')使用selenium(python)找到元素,但无法设置其值,任何人都可以提供帮助,提前感谢!

3 个答案:

答案 0 :(得分:0)

找到input元素后,只需将密钥发送给它:

role_name = driver.find_element_by_css_selector('input[ng-model="selectedRole.roleName"]')
role_name.send_keys("test")

答案 1 :(得分:0)

我认为您需要在使用WebDriverWait的send_keys之前等待,直到元素可见,如下所示:

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

如果仍然无法设置值,请尝试使用execute_script,如下所示:

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

希望它有帮助...:)

答案 2 :(得分:0)

您可以使用以下代码访问和设置值。

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