M03B1U5
M03B1U5

Reputation: 71

Incrementing a string in a input box with Python and Selenium

I am stuck. Can you help me?

I have an input box on a site, where the client number is entered. The number is in format NO0000 (contains both letters and numbers). I want the script to increment NO0000 with 1, depending on if the client number already exists.

For example, if I have NO000, i want the input to add an increment to the last number of the client no, so the output will be NO001.

I want to do this with Selenium webdriver on a page with an input box.

`#input client details
clientno = driver.find_element(by=By.XPATH, value='//*[@id="ctl00_MainContentPlaceHolder_tbclient_no"]')
clientno.send_keys('NO000')
clientno = bytes(clientno, 'utf-8')
clientnoincr = bytes(clientno + 1)
clientnoincr = str(clientnoincr)`

When i do this, nothing happens. Why?

Upvotes: 1

Views: 94

Answers (1)

sound wave
sound wave

Reputation: 3547

Let's say that clientno is the string NO000, then to increment it by 1 you can do

clientno = f'NO{int(clientno[2:])+1:03d}'

where clientno[2:] is '000' since [2:] skips the first two characters of 'NO000'. The string 000 is then converted to a int, which is 0. Then it sums 1 and convert it back to a string, putting leading zeros in such a way that the resulting string has three characters, i.e. f'{4:03d}' is '004' and f'{19:03d}' is '019'

Upvotes: 1

Related Questions