如何选择类更改的“表单webElement”?

问题描述

<div _ngcontent-ucs-c11 class="order__form--name"> ==$0
 <label _ngcontent-ucs-c11 class="order__form--label">Symbol</label>
 <!---->
 <input _ngcontent-ucs-c11 class="form-control form-control-sm ng-untouched ng-pristine ng-invalid" formcontrolname="symbol" type="text" typeahead-editable="false" typeaheadoptionfield="symbolName"typeaheadoptionslimit="50">
 <!---->
 <!---->
</div>

我正在尝试在 selenium (python) 中使用 send_Keys,但无法找到文本 Web 元素。我尝试按类和 xpath 定位:

drive.find_element_by_class_name("form-control form-control-sm ng-pristine ng-invalid ng-star-inserted ng-touched")

driver.find_element_by_xpath("//input[@class='form-control form-control-sm ng-untouched ng-pristine ng-invalid')]")

类从 "form-control form-control-sm ng-pristine ng-invalid ng-star-inserted ng-touched" 变为 "form-control form-control-sm ng-untouched ng-pristine ng-invalid ng-star-inserted"。 我不确定这是否重要,但除此之外,"_ngcontent-ucs-c11" 还更改为 "_ngcontent-uji-c7",并且可能再次更改为其他内容。 另外,它是否与输入中的 typeahead-editable="false" 属性有关?

解决方法

对于动态元素,您可以使用正则表达式。

driver.find_element_by_xpath("//input[contains(@name,'sel')]")

driver.find_element_by_xpath("//input[starts-with (@name,'Tut')]")

driver.find_element_by_xpath("//input[ends-with (@name,'nium')]")

选择一个适合您的情况(您将使用 (@class,'Tut'))。并在 https://www.tutorialspoint.com/how-to-use-regular-expressions-in-xpath-in-selenium-with-python

上阅读更多内容

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...