I have created a bot to auto-update my product prices on Shopify with Selenium Python. I told this bot to locate the price_element clear it, and then send the new prices as a 2 decimal place formated string. e.g. 184.99.
formatted_price = "{:.2f}".format(total_price)
# Scroll down to the price element
self.driver.find_element(By.TAG_NAME, 'body').send_keys(Keys.PAGE_DOWN)
self.driver.find_element(By.TAG_NAME, 'body').send_keys(Keys.PAGE_DOWN)
price_element = self.driver.find_element(By.XPATH, '//input[@name="price"]')
price_element.clear()
price_element.send_keys(formatted_price)
To my surprise, the bot was able to locate the element, but it couldn’t clear it and started adding new prices to the previous prices. The image below shows the previous prices of 162.99, 184.99, and 184.99 combined into the price box.
2
Answers
Seems like a lot of code. Without knowing the rest of your code, how about something like this?
In full disclosure, I’m the author of the Browserist package. Browserist is lightweight, less verbose extension of the Selenium web driver that makes browser automation even easier. Simply install the package with
pip install browserist
and you’re ready to go.Other notes:
price_element.clear()
.browser.scroll.into_view_if_not_in_viewport(...)
line of code.From the looks of it, the effort to clear the price element is probably before it is fully loaded/ accessible. Perhaps you could add a wait condition to ensure the element is ready to be accessed before you attempt interacting with it.