如何解决如何选择类更改的“表单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
上阅读更多内容版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。