python-3.xseleniumselenium-webdriverselenium-chromedriverselenium-rc

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


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

I'm trying to send_Keys in selenium (python) but am unable to locate the text Web Element. I tried locating by class and 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')]")

The class changes from "form-control form-control-sm ng-pristine ng-invalid ng-star-inserted ng-touched" to "form-control form-control-sm ng-untouched ng-pristine ng-invalid ng-star-inserted". I'm not sure if this is significant but, in addition to this the "_ngcontent-ucs-c11" also changed to "_ngcontent-uji-c7" and might change again to something else. Also, is it anyway related to typeahead-editable="false" attribute inside input?


Solution

  • For the dynamic element you can use regular expresions.

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

    or

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

    or

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

    Pick one that suits in your case(you will use (@class,'Tut')). And read more on https://www.tutorialspoint.com/how-to-use-regular-expressions-in-xpath-in-selenium-with-python