emad
emad

Reputation: 3109

setting an input field using xpath

I have a form such as this:

enter image description here

I am trying to set the input field value to a postal code value "M3C1B4" and then I would like to click the button.

Although I can click the button I am unable to set the value of the input field.

I've tried the following code with no success:

driver.execute_script('arguments[0].value = "M3C1B4";', driver.find_element_by_xpath('//div[@class="shippingBox-update"]//input'))
driver.find_element_by_xpath('//div[@class="shippingBox-update"]//input').send_keys('M3C1B4')

Clicking the button works using this code:

driver.find_element_by_xpath('//div[@class="shippingBox-update"]//button').click()

Upvotes: 0

Views: 1121

Answers (2)

emad
emad

Reputation: 3109

Sorry for the trouble. I got it to work. Apart from the error where I had _ instead of - that was pointed out by "guy", there was another problem.

The box was populated by a default value that had to be cleared first because the box only allowed for 6 values.

This did the trick:

driver.find_element_by_xpath('//div[@class="shippingBox-update"]//input').clear()
driver.find_element_by_xpath('//div[@class="shippingBox-update"]//input').send_keys('M3C1B6')
driver.find_element_by_xpath('//div[@class="shippingBox-update"]//button').click()

Upvotes: 1

Guy
Guy

Reputation: 50864

You are using _ instead of - in "shippingBox_update". Should be

driver.find_element_by_xpath('//div[@class="shippingBox-update"]//input').send_keys('M3C1B4')

By the way, why don't you use the <input> class?

driver.find_element_by_class_name("PC_change_input").send_keys('M3C1B4')
driver.find_element_by_class_name("button-submit").click()

Upvotes: 1

Related Questions