在python中使用Selenium使用ng-model查找元素

Kin*_*mes 5 python selenium angularjs selenium-webdriver

我正在尝试在python中使用Selenium自动化AngularJS应用程序。我正在尝试使用ng-modal查找元素。我看过一些与Java相关的文章,其中指出您可以使用以下语句

"//input[@ng-model='yourName']"
Run Code Online (Sandbox Code Playgroud)

我正在尝试在python中做同样的事情

(By.XPATH, "//*/select[@ng-model='yourName']")
Run Code Online (Sandbox Code Playgroud)

但是我找不到该元素。我是否缺少某些东西,或者还有其他处理方法吗?

ale*_*cxe 3

由于这是一个 Angular 应用程序,并且 python-selenium 本身并不等待 Angular“稳定下来”(例如protractorpytractor),因此您需要显式等待元素出现

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

wait = WebDriverWait(driver, 10)
elm = wait.until(EC.presence_of_element_located((By.XPATH, "//select[@ng-model='yourName']")))
Run Code Online (Sandbox Code Playgroud)

也可以看看: