如何将select中的"Form webElement"修改为class?

How to select "Form webElement" in which the class changes?

<div _ngcontent-ucs-c11 class="order__form--name"> ==[=13=]
 <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 元素。我尝试通过 class 和 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')]")

class 由 "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

上阅读更多内容