1

I'm trying to fill a form using selenium where a postcode is entered then a dropdown appears with all the addresses in that postcode appear. I'm taking the data from a spreadsheet. when i split the address to take [0] the house name or number, i want to use this to select from the dropdown the correct address. The dropdown lists the full address, i cannot search for the whole address as the ones in my spreadsheet may differ slightly i.e town may be missing.

How would i select by just matching the house name number?

HNUM = sheet['B1']
HNUM = HNUM.value
HNUM = HNUM.split()
HNUM = HNUM[0]

dropdown = browser.find_element_by_id('confirmAddressLookup')
housenumElem = Select(dropdown)
housenumElem.select_by_value(HNUM)

The above code is not working i figure i need to somehow partially match or extract the addresses, split them, then loop through until a match? from the websites code:

Select your address
THE LONG MILL, ROCHDALE ROAD, GREETLAND, HALIFAX, WEST YORKSHIRE HX4 8AL 16 ROCHDALE ROAD, GREETLAND, HALIFAX, WEST YORKSHIRE HX4 8AL 24 ROCHDALE ROAD, GREETLAND, HALIFAX, WEST YORKSHIRE HX4 8AL 26 ROCHDALE ROAD, GREETLAND, HALIFAX, WEST YORKSHIRE HX4 8AL 59 ROCHDALE ROAD, GREETLAND, HALIFAX, WEST YORKSHIRE HX4 8AL 65 ROCHDALE ROAD, GREETLAND, HALIFAX, WEST YORKSHIRE HX4 8AL 69 ROCHDALE ROAD, GREETLAND, HALIFAX, WEST YORKSHIRE HX4 8AL 77 ROCHDALE ROAD, GREETLAND, HALIFAX, WEST YORKSHIRE HX4 8AL 83 ROCHDALE ROAD, GREETLAND, HALIFAX, WEST YORKSHIRE HX4 8AL

I think i need to loop through each value and search for the house number '24' in this example but i do not know how to implement it.

jo1992
  • 11
  • 2
  • So you said "I'm taking the data from a spreadsheet", but also the selenium? Are you comparing the spreadsheet to what you are looking at in the browser? Also, it would be helpful if you explained what is happening rather than the code "is not working" – pjmaracs Jul 19 '19 at 12:27
  • Please check link: https://stackoverflow.com/questions/29772075/selenium-select-selecting-dropdown-option-by-part-of-the-text – PRERNA PAL Jul 19 '19 at 12:31
  • can you post your html code here? you will have to find the drop down list element using the partial string obtained from excel cell. – Sureshmani Kalirajan Jul 19 '19 at 14:02
  • @pjmaracs yes I am taking the address from a spreadsheet, so firstly i have .split the address in order to first enter the postcode into the relevant box, this then brings up a dropdown list containing all of the addresses within that postcode. For example the address from the postcode i want is: 24 ROCHDALE ROAD, GREETLAND, HALIFAX, WEST YORKSHIRE HX4 8AL I cannot search for the full address to get a match because the addresses in my spreadsheet are not written exactly the same. My spreadsheet address may be 24 ROCHDALE ROAD, Halifax, HX4 8AL so i need to search by the term '24' – jo1992 Jul 20 '19 at 16:09
  • @Sureshmani. I'm adding the html to the original post now – jo1992 Jul 20 '19 at 16:15
  • I don't see the html, not sure if you think you added it. Also, did you look at the linked question above? That looked like what I would do – pjmaracs Jul 21 '19 at 20:57

1 Answers1

0

Instead of iterating the list of options of the select you could consider locating it directly using XPath contains() operator

value = driver.find_element_by_xpath("//option[contains(text(),'24')]").get_attribute("value")
housenumElem.select_by_value(value)

More information: XPath Operators & Functions

Dmitri T
  • 159,985
  • 5
  • 83
  • 133