0

I am automating scripts into selenium python,facing issue in sending keys in selenium searchbox.

code is as below:

contact_old=driver.find_element_by_class_name("consoleRelatedRecord")
time.sleep(2)

search_c=contact_old.find_element_by_xpath('/html/body/div[5]/div[1]/section/div/div/div[1]/div[2]/div/one-record-home-flexipage2/forcegenerated-flexipage_rfq_default_rfq__c/flexipage-record-page-decorator/slot/flexipage-record-home-template-desktop2/div/div[2]/div[2]/slot/slot/flexipage-component2[1]/force-progressive-renderer/slot/slot/flexipage-aura-wrapper/div/article/div/div[2]/div/div/div[1]/div[2]/div/div/div[1]/div/input')

search_c.send_keys(contact_name)           
time.sleep(3)

The xpath is always changing , could you find a better way for send_keys in this SearchBOX?

<input class=" default input uiInput uiInputTextForAutocomplete uiInput--default uiInput--input uiInput uiAutocomplete uiInput--default uiInput--lookup" maxlength="500" role="combobox" id="2197:0" aria-expanded="true" aria-autocomplete="list" type="text" aria-describedby="" aria-haspopup="true" aria-activedescendant="" data-aura-rendered-by="2239:0" data-aura-class="uiInput uiInputTextForAutocomplete uiInput--default uiInput--input uiInput uiAutocomplete uiInput--default uiInput--lookup" data-interactive-lib-uid="8" spellcheck="false" autocomplete="off" autocorrect="off" placeholder="Search contacts..." title="Search contacts...">

enter image description here

JeffC
  • 18,375
  • 5
  • 25
  • 47

2 Answers2

1

Try with the below Xpath:

//input[contains(@title,'Search contacts')]

Or

//input[@title='Search contacts...']

Sooraj
  • 535
  • 2
  • 8
0

The desired element is a ReactJS enabled element so to locate the element you have to induce WebDriverWait for the _element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.default.input.uiInput.uiInputTextForAutocomplete.uiInput--default.uiInput--input.uiInput.uiAutocomplete.uiInput--default.uiInput--lookup[title^='Search contacts'][placeholder^='Search contacts']"))).send_keys(contact_name)
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class=' default input uiInput uiInputTextForAutocomplete uiInput--default uiInput--input uiInput uiAutocomplete uiInput--default uiInput--lookup' and starts-with(@title, 'Search contacts')][starts-with(@placeholder, 'Search contacts')]"))).send_keys(contact_name)
    
  • Note : You have to add the following imports:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
DebanjanB
  • 118,661
  • 30
  • 168
  • 217