0

I am learning selenium by trying to scrape a particular website. I am trying to access the zipcode input field to try and change the zipcode. This is my code so far

try:
    selector = '.btn.hvr-fade'
    button = WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.CSS_SELECTOR, selector)))
    button.click()

    selector = ".btn.btn-default.form-control.ui-select-toggle"
    city_dropdown = WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.CSS_SELECTOR, selector)))
    city_dropdown.click()

    city = 'Mumbai'
    city = city.title()
    city_path = "//a[@class='ui-select-choices-row-inner']//span[contains(text(), '%s')]" % city
    city_element = WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.XPATH, city_path)))
    city_element.click()
    time.sleep(1)

    zipcode = 000001
    zipcode_input_path = "//form[@class='ng-valid-editable ng-invalid ng-invalid-required ng-dirty']//div[@class='form-group area-autocomplete area-select ng-scope']//input[@name='ng- 
    pristine ng-untouched ng-valid-editable ng-empty ng-invalid ng-invalid-required']"
            
    zipcode_input = WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.XPATH, zipcode_input_path)))
    zipcode_input.send_keys(zipcode)

except Exception as e:
    print('scraping could not be completed')
    print(e)
    

From the above code this part fails everytime

zipcode_input_path = "//form[@class='ng-valid-editable ng-invalid ng-invalid-required ng-dirty']//div[@class='form-group area-autocomplete area-select ng-scope']//input[@name='ng-pristine ng-untouched ng-valid-editable ng-empty ng-invalid ng-invalid-required']"

zipcode_input = WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.XPATH, zipcode_input_path)))

I am not sure what I am doing wrong. The XPATH to the best of my knowledge is correct. But the program fails here. I am also not getting a proper exception message. This is the exception that is returned

Message:

Please help me out

EDIT1:

This is the zipcode input box I am trying to target

enter image description here

7
  • Why are you using // in the middle instead of /? Commented Dec 19, 2020 at 6:00
  • @BurningAlcohol Does it make much of a difference? As far as I know using // should search for the required element in the given path right? Commented Dec 19, 2020 at 6:03
  • Did you try replacing them with /, except for the first leading //? And yes they do have a difference, // searches from the root while / searches from the previous given element Commented Dec 19, 2020 at 6:15
  • @BurningAlcohol I tried it just now. I am getting the same error as before Commented Dec 19, 2020 at 6:18
  • zipcode needs to be a string. Commented Dec 19, 2020 at 6:27

1 Answer 1

2

You can simply do this. Change zipcode to a string. As well as getting a simpler id to target.

zipcode = '000001'
zipcode_input_path = "areaselect"
        
zipcode_input = WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.ID, zipcode_input_path)))
zipcode_input.send_keys(zipcode)

Element to target

<input qa="areaInput" typeahead-min-length="3" id="areaselect" ng-change="vm.emptyCheck()" autocomplete="off" name="area" type="text" placeholder="Enter your area / apartment / pincode" ng-model="vm.selectedlocation.query" uib-typeahead="address.label for address in vm.getAreasForCity($viewValue)" typeahead-loading="loadingLocations" typeahead-on-select="vm.onSelect($item)" typeahead-editable="false" typeahead-select-on-blur="true" typeahead-select-on-exact="true" required="" typeahead-no-results="noResultsArea" typeahead-focus-on-select="true" typeahead-wait-ms="200" style="width: 100%; height: 35px; color: rgb(51, 51, 51); padding: 4px 15px;" class="ng-pristine ng-valid-editable ng-empty ng-invalid ng-invalid-required ng-touched" aria-autocomplete="list" aria-expanded="false" aria-owns="typeahead-47-9856">
Sign up to request clarification or add additional context in comments.

2 Comments

Not sure how I missed id for the element. This works perfectly. Thanks a lot
No problem have a nice day.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.