我正在通过抓取特定网站来学习硒。我正在尝试访问邮政编码输入字段,以尝试更改邮政编码。到目前为止,这是我的代码
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)
根据上面的代码,这部分每次都会失败
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)))
我不确定自己在做什么错。据XPATH
我所知,这是正确的。但是程序在这里失败。我也没有收到适当的异常消息。这是返回的异常
Message:
请帮帮我
这是我要定位的邮政编码输入框
您可以简单地做到这一点。将邮政编码更改为字符串。以及获得更简单的ID作为目标。
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)
目标元素
<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">
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句